• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Lua Attempt to index global (a nil value)

Joriku

Working in the mines, need something?
Joined
Jul 16, 2016
Messages
1,088
Solutions
15
Reaction score
382
Location
Sweden
YouTube
Joriku
Trying to rescript an old "Frag Reward" script and running into an error I cannot solve so far.
I changed the storage codes into
Lua:
p:getStorageValue
9d7ec16a4af125ea2bafdd518f88576f.png


the script
Lua:
-- ver. 1.1 2011-12-09
-- author tfs, otland.net/members/andypsylon
local c = {
item = 9020, -- reward itemid
store = 6000, -- storage for Current frags like in login script
tstore = 7000, -- Total Storage value for kills
dstore = 8000, -- Total Storage value for deaths
table = {
{"Smashed!", 189, 1},
{"Dead!", 190, 2},
{"Owned!", 18, 3},
{"Pwnt!", 215, 4}
}
}
function onKill(cid, target)
if(not isPlayer(target)) then return true end

local getStor = p:getStorageValue(cid, c.store)
local rand = math.random(1, #c.table)
local name = getCreatureName(cid)

doCreatureSetStorage(cid, c.store, (getStor + 1))
doCreatureSetStorage(cid, c.tstore, (p:getStorageValue(cid, c.tstore) +1))
doCreatureSetStorage(target, c.dstore, (p:getStorageValue(cid, c.dstore) + 1))

doSendAnimatedText(getThingPosition(target), c.table[rand][1], c.table[rand][2])
doPlayerAddItem(cid, c.item, c.table[rand][3])

if(getStor == 5) then
doBroadcastMessage(name .. " is on killing spree! He killed 5 players!")
elseif(getStor == 10) then
doBroadcastMessage(name .. " is dominating! He killed 10 players!")
elseif(getStor == 25) then
doBroadcastMessage(name .. " is CRAZY! He killed 25 players!")
elseif(getStor == 50) then
doBroadcastMessage(name .. " is UNSTOPPABLE!! He killed 50 players! DO SOMETHING!")
elseif(getStor == 100) then
doBroadcastMessage("Bow down to your new god "..name.." has 100 frags!")
end

return true
end
 
Solution
If you want to rescript it to make it work in 1.x then in notepad hit ctrl+h

Find what:
Lua:
p:getStorageValue(cid,

Replace with:
Lua:
cid:getStorageValue(

Hit Replace All.

And then again ctrl+h

Find what
Lua:
doCreatureSetStorage(cid,

Replace with:
Lua:
cid:setStorageValue(

Hit Replace All.

And last replace, again ctrl+h

Find what
Lua:
doCreatureSetStorage(target,

Replace with:
Lua:
target:setStorageValue(

Hit Replace All.

Now you have to delete this line:
Lua:
doSendAnimatedText(getThingPosition(target), c.table[rand][1], c.table[rand][2])
Becouse doSendA..Txt doesn't exist anymore. You can use it only for numbers.

Instead, use this:
Lua:
target:say(c.table[rand][1], TALKTYPE_MONSTER_YELL)


Now it's gonna...
Change p to cid. Ex: cid:getStorageValue.
Although you shouldn't even be using those params because it's not even a creature id anymore in new versions, it's a creature userdata and using cid intentionally as an object is misleading and bad code. But, that will fix your error.
 
If you want to rescript it to make it work in 1.x then in notepad hit ctrl+h

Find what:
Lua:
p:getStorageValue(cid,

Replace with:
Lua:
cid:getStorageValue(

Hit Replace All.

And then again ctrl+h

Find what
Lua:
doCreatureSetStorage(cid,

Replace with:
Lua:
cid:setStorageValue(

Hit Replace All.

And last replace, again ctrl+h

Find what
Lua:
doCreatureSetStorage(target,

Replace with:
Lua:
target:setStorageValue(

Hit Replace All.

Now you have to delete this line:
Lua:
doSendAnimatedText(getThingPosition(target), c.table[rand][1], c.table[rand][2])
Becouse doSendA..Txt doesn't exist anymore. You can use it only for numbers.

Instead, use this:
Lua:
target:say(c.table[rand][1], TALKTYPE_MONSTER_YELL)


Now it's gonna work in 1.2+
 
Last edited:
Solution
If you want to rescript it to make it work in 1.x then in notepad hit ctrl+h

Find what:
Lua:
p:getStorageValue(cid,

Replace with:
Lua:
cid:getStorageValue(

Hit Replace All.

And then again ctrl+h

Find what
Lua:
doCreatureSetStorage(cid,

Replace with:
Lua:
cid:setStorageValue(

Hit Replace All.

And last replace, again ctrl+h

Find what
Lua:
doCreatureSetStorage(target,

Replace with:
Lua:
target:setStorageValue(

Hit Replace All.

Now you have to delete this line:
Lua:
doSendAnimatedText(getThingPosition(target), c.table[rand][1], c.table[rand][2])
Becouse doSendA..Txt doesn't exist anymore. You can use it only for numbers.

Instead, use this:
Lua:
target:say(c.table[rand][1], TALKTYPE_MONSTER_YELL)


Now it's gonna work in 1.2+
It's not working
Creaturescripts.xml
Lua:
<event type="kill" name="FragReward" script="tfws_creaturescripts/fragreward.lua"/>
fragreward.lua
Lua:
-- ver. 1.1 2011-12-09
-- author tfs, otland.net/members/andypsylon
local c = {
item = 9020, -- reward itemid
store = 6000, -- storage for Current frags like in login script
tstore = 7000, -- Total Storage value for kills
dstore = 8000, -- Total Storage value for deaths
table = {
{"Smashed!", 189, 1},
{"Dead!", 190, 2},
{"Owned!", 18, 3},
{"Pwnt!", 215, 4}
}
}
function onKill(cid, target)
if(not isPlayer(target)) then return true end

local getStor = cid:getStorageValue( c.store)
local rand = math.random(1, #c.table)
local name = getCreatureName(cid)

cid:setStorageValue(c.store, (getStor + 1))
cid:setStorageValue(c.tstore, (cid:getStorageValue(c.tstore) +1))
target:setStorageValue(c.dstore, (cid:getStorageValue(c.dstore) + 1))

target:say(c.table[rand][1], TALKTYPE_MONSTER_YELL)
doPlayerAddItem(cid, c.item, c.table[rand][3])

if(getStor == 5) then
doBroadcastMessage(name .. " is on killing spree! He killed 5 players!")
elseif(getStor == 10) then
doBroadcastMessage(name .. " is dominating! He killed 10 players!")
elseif(getStor == 25) then
doBroadcastMessage(name .. " is CRAZY! He killed 25 players!")
elseif(getStor == 50) then
doBroadcastMessage(name .. " is UNSTOPPABLE!! He killed 50 players! DO SOMETHING!")
elseif(getStor == 100) then
doBroadcastMessage("Bow down to your new god "..name.." has 100 frags!")
end

return true
end
login.lua
end line
Lua:
-- Events
    player:registerEvent("PlayerDeath")
    player:registerEvent("DropLoot")
    player:registerEvent("FragReward")
    return true
    end
No errors in console but nothing happens, no broadcast and/or item given upon kill
 
change:
Lua:
if(not isPlayer(target)) then return true end

to:
Lua:
if not target:isPlayer() then
cid:say("debug: target wasnt a player", 1) -- you can remove this line after you check it's working, but if your char starts spamming this, then bug may be becouse you use it in onKill instead of onPrepareDeath.
return true
end
 
change:
Lua:
if(not isPlayer(target)) then return true end

to:
Lua:
if not target:isPlayer() then
cid:say("debug: target wasnt a player", 1) -- you can remove this line after you check it's working, but if your char starts spamming this, then bug may be becouse you use it in onKill instead of onPrepareDeath.
return true
end
It works now, but it does not broadcast out the killstreak + too many token drops at one death
is it [rand][1] and [rand][3] that doubles the token? (It was) - Solved
Also the msges doesn't show on death -- Not Solved
Smashed
Dead!
Owned!
& Pwnt!
 
Last edited:
Back
Top