• 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!

Solved Attempt to index a nil value problem [TFS 1.2]

Joined
Jul 18, 2014
Messages
193
Solutions
2
Reaction score
15
Hi, i was making this new script for a quest but when i want to use the towers to teleport myself to the right position, in console appears
towersbosses.lua:14: attempt to index a nil value

This is my script: (IT'S TFS 1.2)
Code:
function onUse(player, fromPosition, item, toPosition, itemEx)

local undergrounds = {
[4567] = {newPos = {x = 246, y = 116, z = 11}, creature = "Red Undergrounder", creaturepos = {x = 241, y = 116, z = 11}},
[4568] = {newPos = {x = 283, y = 116, z = 11}, creature = "Green Undergrounder", creaturepos = {x = 277, y = 116, z = 11}},
[4569] = {newPos = {x = 312, y = 116, z = 11}, creature = "Yellow Undergrounder", creaturepos = {x = 304, y = 116, z = 11}},
[4560] = {newPos = {x = 360, y = 116, z = 11}, creature = "Blue Undergrounder", creaturepos = {x = 354, y = 113, z = 11}}
}
local finalboss = {
[4562] = {newPos = {x = 360, y = 116, z = 11}, creature = "Undergrounder", creaturepos = {x = 354, y = 113, z = 11}}
}

    if player:getStorageValue(undergrounds[item.actionid]) < 1 then
        player:teleportTo(undergrounds[item.actionid].newPos)
        doCreatureSay(cid, "Muahahaha!...", TALKTYPE_ORANGE_1)
        if getGlobalStorageValue(undergrounds[item.actionid]) < 1 then
            doSummonCreature(undergrounds[item.actionid].creature, undergrounds[item.actionid].creaturepos)
            setGlobalStorageValue(undergrounds[item.actionid], 1)
        end
    else
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "The cave looks bad, you can't pass there any more.")
    end

    if player:getStorageValue(finalboss[item.uid]) < 1 and player:getStorageValue(undergrounds[item.actionid]) == 2 then
        player:teleportTo(finalboss[item.uid].newPos)
        doCreatureSay(cid, "Death Sentence...", TALKTYPE_ORANGE_1)
        if getGlobalStorageValue(finalboss[item.uid]) < 1 then
            doSummonCreature(finalboss[item.uid].creature, finalboss[item.uid].creaturepos)
            setGlobalStorageValue(finalboss[item.uid], 1)
        end
    elseif player:getStorageValue(finalboss[item.uid]) == 2 then
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "The Undergrounder has been defeated. You can't pass there any more.")
    end
    return true
end

Please help:(
Thanks.
 
This piece of code and code like it in your script is 1 problem
Code:
if player:getStorageValue(undergrounds[item.actionid]) < 1 then
Your asking the server if this entire table is a storage value and that storage value is less then zero.
So basically what you are doing is taking the table's memory address and applying it as a storage value and then asking the server if this particular storage value is less then 1 and this is just 1 error in the script.

Run a print on item.actionid to see what value it holds, i would rewrite this entire script with more checks and balances.
 
This piece of code and code like it in your script is 1 problem
Code:
if player:getStorageValue(undergrounds[item.actionid]) < 1 then
Your asking the server if this entire table is a storage value and that storage value is less then zero.
So basically what you are doing is taking the table's memory address and applying it as a storage value and then asking the server if this particular storage value is less then 1 and this is just 1 error in the script.

Run a print on item.actionid to see what value it holds, i would rewrite this entire script with more checks and balances.

I tried now putting the storage in each one:
Code:
function onUse(player, fromPosition, item, toPosition, itemEx)

local undergrounds = {
[4567] = {newPos = {x = 246, y = 116, z = 11}, creature = "Red Undergrounder", creaturepos = {x = 241, y = 116, z = 11}, storage = 4567},
[4568] = {newPos = {x = 283, y = 116, z = 11}, creature = "Green Undergrounder", creaturepos = {x = 277, y = 116, z = 11}, storage = 4568},
[4569] = {newPos = {x = 312, y = 116, z = 11}, creature = "Yellow Undergrounder", creaturepos = {x = 304, y = 116, z = 11}, storage = 4569},
[4560] = {newPos = {x = 360, y = 116, z = 11}, creature = "Blue Undergrounder", creaturepos = {x = 354, y = 113, z = 11}, storage = 4560}
}
local finalboss = {
[4562] = {newPos = {x = 360, y = 116, z = 11}, creature = "Undergrounder", creaturepos = {x = 354, y = 113, z = 11}, storage = 4562}
}

    if player:getStorageValue(undergrounds[item.actionid].storage) < 1 then
        player:teleportTo(undergrounds[item.actionid].newPos)
        doCreatureSay(cid, "Muahahaha!...", TALKTYPE_ORANGE_1)
        if getGlobalStorageValue(undergrounds[item.actionid].storage) < 1 then
            doSummonCreature(undergrounds[item.actionid].creature, undergrounds[item.actionid].creaturepos)
            setGlobalStorageValue(undergrounds[item.actionid].storage, 1)
        end
    else
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "The cave looks bad, you can't pass there any more.")
    end

    if player:getStorageValue(finalboss[item.uid].storage) < 1 and player:getStorageValue(undergrounds[item.actionid].storage) == 2 then
        player:teleportTo(finalboss[item.uid].newPos)
        doCreatureSay(cid, "Death Sentence...", TALKTYPE_ORANGE_1)
        if getGlobalStorageValue(finalboss[item.uid].storage) < 1 then
            doSummonCreature(finalboss[item.uid].creature, finalboss[item.uid].creaturepos)
            setGlobalStorageValue(finalboss[item.uid].storage, 1)
        end
    elseif player:getStorageValue(finalboss[item.uid].storage) == 2 then
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "The Undergrounder has been defeated. You can't pass there any more.")
    end
    return true
end
But still the same problem. Can you help me with a example of how should be writted? Please.
 
What if you switch item.actionid to item:getActionId()? If it not helps, print the item.actionid to see what it returns you, just as @Codex NG said.
 
What if you switch item.actionid to item:getActionId()? If it not helps, print the item.actionid to see what it returns you, just as @Codex NG said.
item:getActionId() isnt a method :/ but i tried the print with: print(player:getStorageValue(undergrounds[item.actionid])) but in console appears -1 and if a put: print(player:getStorageValue[item.actionid].storage)) appears the error mentioned before
 
item:getActionId() isnt a method :/ but i tried the print with: print(player:getStorageValue(undergrounds[item.actionid])) but in console appears -1 and if a put: print(player:getStorageValue[item.actionid].storage)) appears the error mentioned before
Do only print(item.actionid) please
 
Now when I think of it, should it not be:
Code:
function onUse(player, item, fromPosition, itemEx, toPosition)

.. You have:
Code:
function onUse(player, fromPosition, item, toPosition, itemEx)

Idk if it even matters?
 
Im sure that it will say: "nil" again because it doesnt mean to anything.
I am pretty sure it will not print nil, since it execute when you use the item right? So it is somehow connected with the item.
Just because item.actionid gives nil don't mean item does. IF item do print nil, I would like to see the XML line.
 
I am pretty sure it will not print nil, since it execute when you use the item right? So it is somehow connected with the item.
Just because item.actionid gives nil don't mean item does. IF item do print nil, I would like to see the XML line.
It prints: table: 0x6c339d50
 
if item.actionid is nil then the index values of undergrounds & finalboss are not properly assigned to the item you are using, have you made sure the index values are assigned to actions.xml?
 
if item.actionid is nil then the index values of undergrounds & finalboss are not properly assigned to the item you are using, have you made sure the index values are assigned to actions.xml?
Yes, i put:
Code:
<action fromaid="4567" toaid="4569" script="quests/s quest/towersbosses.lua"/>
    <action actionid="4560" script="quests/s quest/towersbosses.lua"/>
    <action uniqueid="4562" script="quests/s quest/towersbosses.lua"/>
 
1) The order does matter, the item is always the second parameter in onUse: (reference)
Code:
onUse(player, item, fromPosition, target, toPosition, isHotkey)

2) There are no uid or actionid members in an Item object, the valid members (all of them functions) are these: reference. I stand corrected.
 
Last edited:
Back
Top