• 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 santa claus npc error

darkmu

Active Member
Joined
Aug 26, 2007
Messages
253
Solutions
1
Reaction score
45
Location
Paraná,Brazil
Official Topic: NPC - (New) Santa Claus (https://otland.net/threads/new-santa-claus.60905/)

USING TFS 1.3

Lua:
function showTimeLeft(number, usewords)
   local number = tonumber(number)
   if not number then
     return "error"
   end

   if number < 0 then
     return "expired"
   end

   local clocknum = os.date("!%X",number):split(":") -- h:m:s
   local day = math.modf(number / 86400)
   local hour = clocknum[1]
   local minute = clocknum[2]
   local second = clocknum[3]

   if not usewords then
     return table.concat({day, hour, minute, second}, ":")
   end

   local text = {}
   if day > 0 then
     table.insert(text, tonumber(day) .. " day" .. (day > 1 and "s" or ""))
   end

   if hour ~= "00" then
     table.insert(text, tonumber(hour) .. " hour" .. (tonumber(hour) > 1 and "s" or ""))
   end

   if minute ~= "00" then
     table.insert(text, tonumber(minute) .. " minute" .. (tonumber(minute) > 1 and "s" or ""))
   end

   if second ~= "00" then
     table.insert(text, tonumber(second) .. " second" .. (tonumber(second) > 1 and "s" or ""))
   end

   countdown_text = ""
   if #text > 0 then
     countdown_text = text[1]
     for i = 2, #text - 1 do
       countdown_text = countdown_text .. ", " .. text[i]
     end
     if #text > 1 then
       countdown_text = countdown_text .. " and " .. text[#text]
     end
     countdown_text = countdown_text .. ""
   else
     countdown_text = "expired"
   end
return countdown_text
end


local PRESENT_STORAGE = 15726 -- Storage ID
local gifts = {
        {30, 16101, 1}, -- 3% to get XP Boost Pro [30]
        {40, 11211, 1}, -- 4% to get Mount Doll [40]
        {50, 8876, 1}, --  5% to get Addon Dress [50]
        {60, 39999, 1}, --  6% to get Veteran Exercise Doll [60]       
        {70, 36170, 1}, --  7% to get Stamina Refill [70]
        {80, 40000, 1}, --  8% to get Veteran Critical Potion [80]
        {90, 37419, 1}, --  9% to get Skull Remover [90]
        {100, 2160, 100}, -- 10% to get 100 Crystal Coins [100]
        {200, 2688, 10}, -- 20% to get 10 Candy Canes [150]       
        {250, 2111, 5}, -- 25% to get 10 Snowballs [200]
        {300, 2675, 10}, -- 300% to get 10 Orange [250]
        {350, 2674, 15}, -- 35% to get 10 Red Apples [350]
        {500, 2687, 10} -- 50% to get 10 Cookies [500]
    }

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
    
    local player = Player(cid)
    
    local AccountStorage = player:getAccountStorageValue(PRESENT_STORAGE)

    AccountStorage = not AccountStorage and 0 or AccountStorage
    
    if (parameters.present == true) then
        if (player:getAccountStorageValue(PRESENT_STORAGE) > os.time()) then
            selfSay("Do not try to trick me! You have already recieved your present... ".. showTimeLeft(AccountStorage - os.time(), true) ..".", cid)
            return true
        end
        
        if (player:getLevel() < 400) then
            selfSay("Do not try to trick me! You not have level to recieved your present... (LEVEL 400)", cid)
            return true
        end   
        

        local item = {}
        local reward = 0
        local count = ""
        for i = 1, #gifts do
            item = gifts[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
        
        player:addItem(reward, subType)
        player:setStorageValue(PRESENT_STORAGE, 1)
        player:setAccountStorageValue(PRESENT_STORAGE, os.time() + 7200)   
        npcHandler:say('HO-HO-HO! I have ' .. count .. ItemType(reward):getName() .. ' for you.', cid)
    else
        npcHandler:say('Come back when you start behaving.', cid)
    end
    
    npcHandler:resetNpc()
    return true
end
 
npcHandler:setMessage(MESSAGE_GREET, "HO-HO-HO, Merry Christmas |PLAYERNAME|. I have presents for the good children.")

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

local node = keywordHandler:addKeyword({'present'}, StdModule.say, {npcHandler = npcHandler, onlyFocus = true, text = 'Have you been well behaved and good this year?...'})
    node:addChildKeywordNode(yesNode)
    node:addChildKeywordNode(noNode)

npcHandler:addModule(FocusModule:new())

Code:
function Player.getAccountStorageValue(self, key)
    local toNumber = tonumber(key)
    if not toNumber then
        return false
    end
    local query = db.storeQuery("SELECT `value` FROM `account_storage` WHERE `account_id` = ".. self:getAccountId() .." AND `key` = ".. key)
    if not query then
        return false
    end

    local value = result.getDataInt(query, "value")
    result.free(query)
    return value
end

function Player.setAccountStorageValue(self, key, value)
     local toNumber = tonumber(key)
    if not toNumber then
        return false
    end
    local query = ""
    if self:getAccountStorageValue(key) ~= -1 then
        query = ("UPDATE `account_storage` SET `value` = %d WHERE `account_id` = %d AND `key` = %d"):format(value, self:getAccountId(), key)
    else
        query = ("INSERT INTO `account_storage` (`account_id`, `key`, `value`) VALUES (%d, %d, %d)"):format(self:getAccountId(), key, value)
    end
    return db.query(query)
end

ERROR:
C++:
2020-12-23 13:46:30 -  Lua Script Error: [Npc interface]
2020-12-23 13:46:30 -  data/npc/scripts/santa.lua:onCreatureSay
2020-12-23 13:46:30 -  data/npc/scripts/santa.lua:94: attempt to compare number with boolean
2020-12-23 13:46:30 -  stack traceback:
2020-12-23 13:46:30 -   [C]: in function '__lt'
2020-12-23 13:46:30 -   data/npc/scripts/santa.lua:94: in function 'callback'
2020-12-23 13:46:30 -   data/npc/lib/npcsystem/keywordhandler.lua:31: in function 'processMessage'
2020-12-23 13:46:30 -   data/npc/lib/npcsystem/keywordhandler.lua:186: in function 'processNodeMessage'
2020-12-23 13:46:30 -   data/npc/lib/npcsystem/keywordhandler.lua:154: in function 'processMessage'
2020-12-23 13:46:30 -   data/npc/lib/npcsystem/npchandler.lua:428: in function 'onCreatureSay'
2020-12-23 13:46:30 -   data/npc/scripts/santa.lua:78: in function <data/npc/scripts/santa.lua:78>

WHEN DEBUG player:getAccountStorageValue(PRESENT_STORAGE), return false for me and not os.time :(
Post automatically merged:

Fixed

SOLUTION LINK ABOVE:
 
Last edited:
Top