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

TFS 1.X+ [LUA][Newbie] ScriptHelp - Local variables resets and more

Klank

Althea ¤ A New World Developer
Joined
Feb 21, 2008
Messages
1,081
Reaction score
643
Location
Norway
Hello,
My plan is two click on two different walls and a stairwell will appear. I'm in the phase of learning scripting and i know my code is super crap..
However, i almost got it to work. The problem i am facing is that "local Wall1" and "local Wall2" resets to 0 every time I activate the script (clicking on the second wall sets wall1 back to 0 and vice versa) , can someone show me a better way of handling this scenario?
There must be a much easier and simpler way of doing this, but i don't know how to do it yet :p
Any help is appreciated.

(NVM the local config, i tested this using a table at first)

Lua:
local config = {
    --["Wall1"] = 0, ["Wall2"] = 0
}
local Wall1 = 0
local Wall2 = 0

local stairPosition = Position(734, 450, 11)

function onUse(cid, item, fromPosition, target, toPosition, isHotkey)
    if item.itemid == 8506 and Wall1 ~= 1 then
        doCreatureSay(cid, "Wall1 is set to 1", TALKTYPE_MONSTER_SAY, true, 0, fromPosition)
        Wall1 = 1
    elseif Wall1 == 1 and item.itemid == 8506 then
        doCreatureSay(cid, "You already used this.", TALKTYPE_MONSTER_SAY, true, 0, fromPosition)
    end

    if item.itemid == 8521 and Wall2 ~= 1 then
        doCreatureSay(cid, "Wall2 is set to 2", TALKTYPE_MONSTER_SAY, true, 0, fromPosition)
        Wall2 = 1
    elseif Wall2 == 1 and item.itemid == 8521 then
        doCreatureSay(cid, "You already used this.", TALKTYPE_MONSTER_SAY, true, 0, fromPosition)
    end


    local StairTile = Tile(stairPosition):getItemById(3153)
    local test = Wall1 * Wall2
    print(test)

    if test == 1 then
        StairTile:transform(4835)
        stairPosition:sendMagicEffect(CONST_ME_POFF)
    end

    addEvent(function(TilePosition, toPosition,TileID)
        local StairTile2 = Tile(toPosition):getItemById(TileID)
        if StairTile2 then
            Stairtile2:transform(3153)
            TilePosition:sendMagicEffect(CONS_ME_POFF)
        end
    end,10000, stairPosition, toPosition, 4835)

    return true
end
 
Hello,
My plan is two click on two different walls and a stairwell will appear. I'm in the phase of learning scripting and i know my code is super crap..
However, i almost got it to work. The problem i am facing is that "local Wall1" and "local Wall2" resets to 0 every time I activate the script (clicking on the second wall sets wall1 back to 0 and vice versa) , can someone show me a better way of handling this scenario?
There must be a much easier and simpler way of doing this, but i don't know how to do it yet :p
Any help is appreciated.

(NVM the local config, i tested this using a table at first)

Lua:
local config = {
    --["Wall1"] = 0, ["Wall2"] = 0
}
local Wall1 = 0
local Wall2 = 0

local stairPosition = Position(734, 450, 11)

function onUse(cid, item, fromPosition, target, toPosition, isHotkey)
    if item.itemid == 8506 and Wall1 ~= 1 then
        doCreatureSay(cid, "Wall1 is set to 1", TALKTYPE_MONSTER_SAY, true, 0, fromPosition)
        Wall1 = 1
    elseif Wall1 == 1 and item.itemid == 8506 then
        doCreatureSay(cid, "You already used this.", TALKTYPE_MONSTER_SAY, true, 0, fromPosition)
    end

    if item.itemid == 8521 and Wall2 ~= 1 then
        doCreatureSay(cid, "Wall2 is set to 2", TALKTYPE_MONSTER_SAY, true, 0, fromPosition)
        Wall2 = 1
    elseif Wall2 == 1 and item.itemid == 8521 then
        doCreatureSay(cid, "You already used this.", TALKTYPE_MONSTER_SAY, true, 0, fromPosition)
    end


    local StairTile = Tile(stairPosition):getItemById(3153)
    local test = Wall1 * Wall2
    print(test)

    if test == 1 then
        StairTile:transform(4835)
        stairPosition:sendMagicEffect(CONST_ME_POFF)
    end

    addEvent(function(TilePosition, toPosition,TileID)
        local StairTile2 = Tile(toPosition):getItemById(TileID)
        if StairTile2 then
            Stairtile2:transform(3153)
            TilePosition:sendMagicEffect(CONS_ME_POFF)
        end
    end,10000, stairPosition, toPosition, 4835)

    return true
end
The problem you are facing is that your wall variables works only during script time. So if you use wall, it checks something but in time you click on another wall the script will be be executed again and setting Wall1/Wall2 = 0.
You can put your Wall2/Wall1 = 0 to your global.lua. Global.lua is executed only once at startup. So the server will remember that you set it to 1 previously
 
The problem you are facing is that your wall variables works only during script time. So if you use wall, it checks something but in time you click on another wall the script will be be executed again and setting Wall1/Wall2 = 0.
You can put your Wall2/Wall1 = 0 to your global.lua. Global.lua is executed only once at startup. So the server will remember that you set it to 1 previously
Hey, thanks that solved the issue, i did not put anything into the global.lua but just removed "local" infront, but that might be the same thing.. I have evolved the script some more and facing a new issue.
Scripts works fine, but if i click the wall within the 10 seconds the stair shall appear im getting nil value on "local StairTile" since it tries to get item 3153, which does not exist at the position for 10 seconds. Do you, or anyone else, have a better way of getting this item ID?

Lua:
 DwarfWall1 = 0
 DwarfWall2 = 0
local EventId = nil
local stairPosition = Position(734, 450, 11)
function onUse(cid, item, fromPosition, target, toPosition, isHotkey)
    if item.itemid == 8506 and DwarfWall1 ~= 1 then
        doCreatureSay(cid, "Some mechanism was activated..", TALKTYPE_MONSTER_SAY, true, 0, fromPosition)
        DwarfWall1 = 1
        doSendMagicEffect(fromPosition,CONST_ME_MAGIC_RED)
    elseif item.itemid == 8506 and DwarfWall1 == 1 then
        doCreatureSay(cid, "You already used this.", TALKTYPE_MONSTER_SAY, true, 0, fromPosition)

    end

    if item.itemid == 8521 and DwarfWall2 ~= 1 and DwarfWall1 == 1 then
        doCreatureSay(cid, "A stair appears!", TALKTYPE_MONSTER_SAY, true, 0, fromPosition)
        DwarfWall2 = 1
        doSendMagicEffect(fromPosition,CONST_ME_MAGIC_RED)
    elseif item.itemid == 8521 and DwarfWall2 == 1 then
        doCreatureSay(cid, "You already used this.", TALKTYPE_MONSTER_SAY, true, 0, fromPosition)

    end


    local WallStatus = DwarfWall1 * DwarfWall2
    if WallStatus == 1  then
        local StairTile = Tile(stairPosition):getItemById(3153)
        StairTile:transform(4835)
        stairPosition:sendMagicEffect(CONST_ME_POFF)

        EventId = addEvent(function(pos,item)
            local stairtile2 = Tile(pos):getItemById(item)
            if stairtile2 then
                stairtile2:transform(3153)
                DwarfWall1 = 0
                DwarfWall2 = 0
                stairPosition:sendMagicEffect(CONST_ME_POFF)
            end
        end , 10000, stairPosition, 4835)
    else
        stopEvent(EventId)
    end

    return true
end
 
You should add 'if StairTile then' before transform function to check if StairTile is not null
like this

Lua:
local WallStatus = DwarfWall1 * DwarfWall2
   if WallStatus == 1  then
       local StairTile = Tile(stairPosition):getItemById(3153)
       if StairTile then -- here is this if
            StairTile:transform(4835)
            stairPosition:sendMagicEffect(CONST_ME_POFF)

            EventId = addEvent(function(pos,item)
                local stairtile2 = Tile(pos):getItemById(item)
                if stairtile2 then
                    stairtile2:transform(3153)
                    DwarfWall1 = 0
                    DwarfWall2 = 0
                    stairPosition:sendMagicEffect(CONST_ME_POFF)
                end
            end , 10000, stairPosition, 4835)
        end -- end to close new if
   else
       stopEvent(EventId)
   end
 
You should add 'if StairTile then' before transform function to check if StairTile is not null
like this

Lua:
local WallStatus = DwarfWall1 * DwarfWall2
   if WallStatus == 1  then
       local StairTile = Tile(stairPosition):getItemById(3153)
       if StairTile then -- here is this if
            StairTile:transform(4835)
            stairPosition:sendMagicEffect(CONST_ME_POFF)

            EventId = addEvent(function(pos,item)
                local stairtile2 = Tile(pos):getItemById(item)
                if stairtile2 then
                    stairtile2:transform(3153)
                    DwarfWall1 = 0
                    DwarfWall2 = 0
                    stairPosition:sendMagicEffect(CONST_ME_POFF)
                end
            end , 10000, stairPosition, 4835)
        end -- end to close new if
   else
       stopEvent(EventId)
   end

ahh thanks man... Now it works perfectly ! Appriciate the help!
 
Back
Top