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

Lua [TFS 1.0] NPC (onCreatureSay)

Sentinel3

New Member
Joined
Oct 16, 2014
Messages
180
Reaction score
0
Hi everyone,

Well you know guys that Christmas is here soon :rolleyes:, so I decided to create the NPC (Santa Claus) for reward the players who connect during Christmas.

About NPC, all works perfect except this error:

Using: Lastest version of TFS 1.0

Error I got in console:
HTML:
Lua Script Error: [Npc interface]
data/npc/scripts/Santa Claus.lua:onCreatureSay
data/npc/scripts/Santa Claus.lua:54: attempt to call global 'getItemNameById' (a nil value)
stack traceback:
        [C]: in function 'getItemNameById'
        data/npc/scripts/Santa Claus.lua:54: in function 'callback'
        data/npc/lib/npcsystem/keywordhandler.lua:26: in function 'processMessage'
        data/npc/lib/npcsystem/keywordhandler.lua:135: in function 'processNodeMessage'
        data/npc/lib/npcsystem/keywordhandler.lua:103: in function 'processMessage'
        data/npc/lib/npcsystem/npchandler.lua:401: in function 'onCreatureSay'
        data/npc/scripts/Santa Claus.lua:23: in function <data/npc/scripts/Santa Claus.lua:23>

NPC (Santa Claus) /data/npc/scripts/Santa Claus.lua
HTML:
random_items = {
{5,2112,1}, -- 0.5% to get teddy bear
{20,6512,1}, -- 2% to get santa doll
{40,2114,1}, -- 4% to get piggy bank
{80,2111,5}, -- 8% to get 5 snowballs
{80,2688,8}, -- 8% to get 8 candy canes
{80,2110,1}, -- 8% to get doll
{400,2674,15}, -- 40% to get 15 red apples
{450,2675,10}, -- 45% to get 10 oranges
{1000,2687,8} -- 100% to get 8 cookies
}
PRESENT_STORAGE = 54163 -- storage ID



    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

function santaNPC(cid, message, keywords, parameters, node)
        if(not npcHandler:isFocused(cid)) then
    return false
end

    if (parameters.present == true) then
    if (getPlayerStorageValue(cid, PRESENT_STORAGE) < 1) then

    local item = {}
    local reward = 0
    local count = ""
    
        for i = 1, #random_items do
            item = random_items[i]
        if (math.random(0,999) < item[1]) then
            reward = item[2]
            subType = item[3]

        if subType > 1 then
            count = subType .. " "
    end

break
end
end

        doPlayerAddItem(cid, reward, subType)
        setPlayerStorageValue(cid, PRESENT_STORAGE, 1)
        npcHandler:say('HO HO HO to be good this year I will give you ' .. count .. getItemByNameId(reward) .. ' for you.', cid)
    else
        npcHandler:say('I already gave you the gift, come back next year. HO HO HO!', cid)
end
    else
        npcHandler:say('Come back when you being a good guy.', cid)
end
        npcHandler:resetNpc()
    return true
end

        npcHandler:setMessage(MESSAGE_GREET, "HO HO HO I am Santa Claus. Merry Christmas |PLAYERNAME|!. I will give you a gift for being a good guy.")

    local noNode = KeywordNode:new({'no'}, santaNPC, {present = false})
    local yesNode = KeywordNode:new({'yes'}, santaNPC, {present = true})

    local node = keywordHandler:addKeyword({'pre'}, StdModule.say, {npcHandler = npcHandler, onlyFocus = true, text = 'You were good this year?'})
          node:addChildKeywordNode(yesNode)
          node:addChildKeywordNode(noNode)
          npcHandler:addModule(FocusModule:new())

Thank you so much, I hope that someone can help me! :p
 
Back
Top