• 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 AddEvent - Callback parameter should be a function.

Harter

New Member
Joined
Mar 4, 2013
Messages
5
Reaction score
1
So im trying to add an addevent in onUse function. But it doesnt work :(
Do someone have any idea why? I will paste script here:
Code:
local config = {
    normalLever = 1945,
    pulledLever = 1946,
    leverUid = 1200, 
    messageRemoved = "A stone is removed in the distance!",
    messagePlaced = "The stone is placed back!",
    stonePos = {x=224, y=354, z=5, stackpos=1},
    stoneID = 1358,
    delay = 1
}

function onUse(cid, item, fromPos, itemEx, toPos)
    stone = getThingfromPos(config.stonePos)
    if item.itemid == config.normalLever and item.uid == config.leverUid then
        doRemoveItem(stone.uid, 1)
        doTransformItem(item.uid, config.pulledLever)
        doPlayerSendTextMessage(cid, 22, config.messageRemoved)
        addEvent(placeStone, item, cid, config.delay * 5000)
    end
    return true
end

function mapAreas(fromPos, toPos, stack)
    local myTable = {}
    for _x = fromPos.x, toPos.x do
        for _y = fromPos.y, toPos.y do
            local thing = getThingFromPos({x = _x, y = _y, z = fromPos.z, stackpos = stack})
            if isPlayer(thing.uid) then
                table.insert(myTable, thing.uid)
            end
        end
    end
    return #myTable > 0 and myTable or nil
end

function placeStone(placeStone, item, cid)
    local fromPos = stonePos
    local toPos = stonePos
    local thing = mapAreas(fromPos, toPos, 253)
    if thing then
        local pos = getPlayerPosition(cid)
        doTeleportThing(cid, {x=pos.x, y=pos.y-1, z=pos.z})
        doCreateItem(config.stoneID, 1, config.stonePos)
        doPlayerSendTextMessage(cid, 22, config.messagePlaced)
    else
        doCreateItem(config.stoneID, 1, config.stonePos)
        doPlayerSendTextMessage(cid, 22, config.messagePlaced)
    end
    return true
end
 
Last edited by a moderator:
addEvent(placeStone, item, cid, config.delay * 5000)
what the f is that?
either write the function inside the addEvent or first value is function name is function, then delay and then parameters.

how to write function inside addEvent?
Code:
addEvent(function() print("very simple!") end, 1000)

but in your case just do this:
Code:
addEvent(placeStone, config.delay * 5000, item, cid)
 
Well, the function has to exist before you can use it, and as you can see in this script, your function placeStone is defined after the addEvent which calls it, meaning that the script still doesn't recognize which function it is at that point. So it would be wise to move the function somewhere above the addEvent line that uses it.
 
The function placeStone is defined as
Code:
function placeStone(placeStone, item, cid)
taking 3 parameters, placeStone(unused), item(unused) and cid.

In addEvent, as pointed out by @whitevo, the arguments are in the wrong order, but aside from that you're only passing item and cid. This results in that inside the placeStone function, the variables would be; placeStone = item, item = cid, cid = nil.

Since the parameters placeStone and item is never used you can just remove them from the function definition.
Code:
function placeStone(cid)
Code:
addEvent(placeStone, config.delay * 5000, cid)
Like this placeStone only expects 1 parameter, cid, and you pass only cid in the call to addEvent.
 
Hope I didn't break it :)
Code:
local c = {
    normalLever = 1945,
    pulledLever = 1946,
    leverUid = 1200,
    messageRemoved = "A stone is removed in the distance!",
    messagePlaced = "The stone is placed back!",
    stonePos = {x=224, y=354, z=5, stackpos=1},
    stoneID = 1358,
    delay = 5
}

function onUse(cid, item, fromPos, itemEx, toPos)
    -- no arguments needed since its a local function
    local function placeStone()
        -- the stonePos was not defined so i sent c.stonePos as arguments
        local thing = mapAreas(c.stonePos, c.stonePos, 253)
        if thing then
            local pos = getPlayerPosition(cid)
            doTeleportThing(cid, {x = pos.x, y = pos.y - 1, z = pos.z})
        end
        -- this will be called regardless so no need for else
        doCreateItem(c.stoneID, 1, c.stonePos)
        doPlayerSendTextMessage(cid, 22, c.messagePlaced)
        -- return true isn't needed
    end
 
    local stone = getThingfromPos(c.stonePos)
    if item.itemid == c.normalLever and item.uid == c.leverUid then
        doRemoveItem(stone.uid, 1)
        doTransformItem(item.uid, c.pulledLever)
        doPlayerSendTextMessage(cid, 22, c.messageRemoved)
        addEvent(placeStone, c.delay * 1000)
    end
    return true
end

-- this seems like a wasted function
function mapAreas(fromPos, toPos, stack)
    local myTable = {}
    for _x = fromPos.x, toPos.x do
        for _y = fromPos.y, toPos.y do
            local thing = getThingFromPos({x = _x, y = _y, z = fromPos.z, stackpos = stack})
            if isPlayer(thing.uid) then
                table.insert(myTable, thing.uid)
            end
        end
    end
    --return #myTable > 0 and myTable or nil
    -- since nothing is being done with myTable, just return true or false
    return #myTable > 0
end
 
THank you guys for your quick response. I didnt knew about addevent which has to be before the function. Im doing lua scripting since 2-3 weeks but it does not justify me. Codex Ng, i have copied your script and addevent is working but not properly. When i click on the lever, stone disappears but after 5 sec i get following bug: stone.lua:42: attempt to call global 'getThingFromPos' <a nil value>.
Whats nil value?
 
THank you guys for your quick response. I didnt knew about addevent which has to be before the function. Im doing lua scripting since 2-3 weeks but it does not justify me. Codex Ng, i have copied your script and addevent is working but not properly. When i click on the lever, stone disappears but after 5 sec i get following bug: stone.lua:42: attempt to call global 'getThingFromPos' <a nil value>.
Whats nil value?
Nil means no value
 
Back
Top