• 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 NPC setCallback don't receive creature id.

maxilos

New Member
Joined
Nov 1, 2009
Messages
7
Solutions
1
Reaction score
3
Hello everyone!
I have problem with recieving creature id on farewell and creature disappear callback in lua function.

Here is my lua code:
LUA:
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)

function onCreatureAppear(cid) npcHandler:onCreatureAppear(cid) end
function onCreatureDisappear(cid) npcHandler:onCreatureDisappear(cid) end
function onCreatureSay(cid, type, msg) npcHandler:onCreatureSay(cid, type, msg) end
function onThink() npcHandler:onThink() end

local playersQueue = {}
local actualPlayer = 0
local nextPlayer = 0

local function queueService(cid)
    for k,v in pairs(playersQueue) do
        if v == cid then
            table.remove(playersQueue, k)
            print('remove')
        else
            print(cid .. ' not found in queue')
        end
    end
  
    if actualPlayer == cid then
        actualPlayer = 0
    end
  
    if #playersQueue > 0 and actualPlayer == 0 then
        nextPlayer = playersQueue[1]
        table.remove(playersQueue, 1)
        local player = Player(nextPlayer)
        npcHandler:say("Thanks for waiting " .. player:getName() .. "!", nextPlayer)
        actualPlayer = nextPlayer
        nextPlayer = 0
    end
  
    return true
end

local function greetCallback(cid)
    if #playersQueue < 1 and actualPlayer == 0 then
        npcHandler:setMessage(MESSAGE_GREET, "Welcome |PLAYERNAME|!")
        actualPlayer = cid
    else
        npcHandler:setMessage(MESSAGE_GREET, "Welcome |PLAYERNAME|! I'm already busy. I add you to queue. Please wait for your turn.")
        table.insert(playersQueue, cid)
    end
    return true
end

local function creatureSayCallback(cid, type, msg)
    local player = Player(cid)
  
    if msgcontains(msg, "queueCount") then
        npcHandler:say("Queue count is: " .. #playersQueue, cid)
    elseif msgcontains(msg, "queueContent") then
        for k,v in pairs(playersQueue) do
            selfSay(k .. ' => ' .. v, cid)
        end
    end
end

local function farewellCallback(cid)
    queueService(cid)
    npcHandler:setMessage(MESSAGE_FAREWELL, "Good bye |PLAYERNAME|. See you next time!")
    return true
end

local function disappearCallback(cid)
    queueService(cid)
    npcHandler:setMessage(MESSAGE_WALKAWAY, "Good bye. See you next time!")
    return true
end

npcHandler:setCallback(CALLBACK_GREET, greetCallback)
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:setCallback(CALLBACK_FAREWELL, farewellCallback)
npcHandler:setCallback(CALLBACK_CREATURE_DISAPPEAR, disappearCallback)

npcHandler:addModule(FocusModule:new())

When I talk 'bye' to NPC , I get a bug in the console:
Code:
Lua Script Error: [Npc interface]
data/npc/scripts/TestNPC.lua:onThink
data/npc/scripts/TestNPC.lua:20: attempt to concatenate local 'cid' (a nil value)
stack traceback:
        [C]: in function '__concat'
        data/npc/scripts/TestNPC.lua:20: in function 'queueService'
        data/npc/scripts/TestNPC.lua:64: in function 'callback'
        data/npc/lib/npcsystem/npchandler.lua:335: in function 'unGreet'
        data/npc/lib/npcsystem/npchandler.lua:524: in function 'onThink'
        data/npc/scripts/TestNPC.lua:8: in function <data/npc/scripts/TestNPC.lua:8>

Only when i logged out function work fine. Why it's happedns? Maybe i not see some bug in code. Please help me. I'm using TFS 1.1 on linux.
 
Solution
I found a solution.
For others who have same problem:

1. Open npchandler.lua
2. Find 335 and 553 line and
replace this:
LUA:
if callback == nil or callback() then
to this:
LUA:
if callback == nil or callback(cid) then
I found a solution.
For others who have same problem:

1. Open npchandler.lua
2. Find 335 and 553 line and
replace this:
LUA:
if callback == nil or callback() then
to this:
LUA:
if callback == nil or callback(cid) then
 
Solution
Back
Top