• 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!
  • 2026 staff recruitment is open! Check it out and consider applying!

Lua getItemById Error

  • Thread starter Thread starter Deleted member 141899
  • Start date Start date
D

Deleted member 141899

Guest
Hello, im using Tfs 1.1, i have an error on my script "gapWagons" in beregar quest:

When i restart the server the script dont work and get an error saying: attempt to index a upvalue 'tile' line:
Code:
 if player:getStorageValue(Storage.hiddenCityOfBeregar.RoyalRescue) == 1 and tile:getItemById(7122) then

if i /reload actions, the scripts works lol, why?

Code:
local tile = Tile(Position(32571, 31508, 9))

function onUse(player, item, fromPosition, target, toPosition, isHotkey)

    if player:getStorageValue(Storage.hiddenCityOfBeregar.RoyalRescue) == 1 and tile:getItemById(7122) then
        player:setStorageValue(Storage.hiddenCityOfBeregar.RoyalRescue, 2)
        player:teleportTo(Position(32578, 31507, 9))
        player:getPosition():sendMagicEffect(CONST_ME_TELEPORT)
        player:say("You safely passed the gap but your bridge collapsed behind you.", TALKTYPE_MONSTER_SAY)
        if tile then
            local thing = tile:getItemById(7122)
            if thing then
                thing:remove()
            end
            local secondThing = tile:getItemById(5779)
            if secondThing then
                secondThing:remove()
            end
        end
    else
        player:teleportTo(Position(32580, 31487, 9))
        player:getPosition():sendMagicEffect(CONST_ME_TELEPORT)
        player:say("You need to build a bridge to pass the gap.", TALKTYPE_MONSTER_SAY)
    end
    return true
end

please help!
 
Last edited by a moderator:
The reason is very simple, but might not be very intuitive. When a script is loaded, the entire body of that script is treated as a function and executed, which means that the variable tile is evaluated at script load time. If you take a look at the TFS log, you'll see that script systems are loaded before the map, so that means that the tile does NOT exist when the script is first loaded.

As a sidenote: storing userdata in non function-local variables is potentially dangerous and might crash the server. In this case it's not a big deal, because Tiles are stable(they can be added but are never removed). Please be careful!
 
I have many scripts with this function, and it seems that usually work, an example is the script of rats bridge of Rookgaard
 
Back
Top