• 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

Fortera Global

Intermediate OT User
Joined
Nov 20, 2015
Messages
1,180
Solutions
2
Reaction score
117
Hello, anyone fix this for TFS 1.2+ please?


Code:
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! You were good like a little dwarf this year! I got ' .. count .. getItemNameById(reward) .. ' for you.', cid)
        else
            npcHandler:say('I gave you a present already.', cid)
        end
    else
        npcHandler:say('Come back when you start behaving good.', cid)
    end
    npcHandler:resetNpc()
    return true
end

npcHandler:setMessage(MESSAGE_GREET, "Merry Christmas |PLAYERNAME|. I'm Santa Claus. I got presents for good children.")

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 = 'Were you good this year?'})
    node:addChildKeywordNode(yesNode)
    node:addChildKeywordNode(noNode)
npcHandler:addModule(FocusModule:new())
 
You can try this, I haven't tested it, but it's a start.
Code:
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

local function creatureSayCallback(cid, type, msg)

    if not npcHandler:isFocused(cid) then
        return false
    end

    local player = Player(cid)

    if msgcontains(msg, "present") and npcHandler.topic[cid] == 0 then
        if player:getStorageValue(PRESENT_STORAGE) == 1 then
            npcHandler:say('I gave you a present already.', cid)
            return false
        else
            npcHandler:say("Were you good this year?", cid)
            npcHandler.topic[cid] = 1   
        end       
    elseif msgcontains(msg, "yes") and npcHandler.topic[cid] == 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
        player:addItem(reward, subType)
        player:setStorageValue(PRESENT_STORAGE, 1)
    elseif msgcontains(msg, "no") then
        npcHandler:say('Come back when you start behaving good.', cid)
    else
        npcHandler:say("What? I can't hear you.")
    end
    return true
end   

npcHandler:setMessage(MESSAGE_GREET, "Merry Christmas |PLAYERNAME|. I'm Santa Claus. I got {presents} for good children.")
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())
 
Hello, this is my first post, so, sorry if in wrong area. I have a santa script as well, and I'm wanting to give it a timer so that every day he will give another item. So far I've been messing around with the timing for quite a while and searching for answers, also integrating "A Sweaty Cyclops.lua" amulet part, and so far I've come to this, I feel like I'm so close lol(could be ages away :/). I've got enough 'OT Scripting' knowledge to do quite a bit, but this is the first timing stuff I've attempted. (It's set to 5 seconds for testing).

The error at the moment is he will give another item immediately after the first. Also tested with god and normal character, no errors in console. TFS 1.2

Script so far:

local random_items = {
{chance = 5, itemid = 2112}, -- 0.5% to get teddy bear
{chance = 20, itemid = 6512}, -- 2% to get santa doll
{chance = 40, itemid = 2114}, -- 4% to get piggy bank
{chance = 40, itemid = 2152, count = 100}, -- 4% to get 100 platinum coins
{chance = 80, itemid = 2111, count = 5}, -- 8% to get 5 snowballs
{chance = 80, itemid = 2688, count = 8}, -- 8% to get 8 candy canes
{chance = 80, itemid = 2110, count = 1}, -- 8% to get doll
{chance = 400, itemid = 2674, count = 15}, -- 40% to get 15 red apples
{chance = 450, itemid = 2675, count = 10}, -- 45% to get 10 oranges
{chance = 1000, itemid = 2687, count = 8} -- 100% to get 8 cookies
}

local PRESENT_STORAGE = 54163 -- storage ID
local PRESENT_TIMER = 54164

local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)


function onCreatureAppear(cid) npcHandler:eek:nCreatureAppear(cid) end
function onCreatureDisappear(cid) npcHandler:eek:nCreatureDisappear(cid) end
function onCreatureSay(cid, type, msg) npcHandler:eek:nCreatureSay(cid, type, msg) end
function onThink() npcHandler:eek:nThink() end

local voices = {
{ text = 'HO HO HO! MERRY CHRISTMAS', yell = true },
{ text = 'Hi there young ones, have you been good this year?' }
}

function santaNPC(cid, message, keywords, present, node)
if not npcHandler:isFocused(cid) then
return false
end
if not present then
npcHandler:say('Come back when you start behaving good.', cid)
return true
end
local player = Player(cid)
if player:getStorageValue(PRESENT_STORAGE) == 1 then
npcHandler:say('You\'ve already received a present |PLAYERNAME|.', cid)
elseif player:getStorageValue(PRESENT_TIMER) + 5 * 1000 > os.time() then
player:setStorageValue(PRESENT_STORAGE, -1)
end
local player = Player(cid)
local item, reward = nil, {}
for i = 1, #random_items do
item = random_items
if math.random(0, 1000) <= item.chance then
reward.itemid = item.itemid
reward.subType = item.count or 1
break
end
end
player:addItem(reward.itemid, reward.subType)
player:setStorageValue(PRESENT_STORAGE, 1)
player:setStorageValue(PRESENT_TIMER, os.time())
npcHandler:say(string.format('HO HO HO! You were good like a little dwarf this year! I got %d %s for you.', reward.subType, ItemType(reward.itemid):getName()), cid)
npcHandler:resetNpc()
return true
end

npcHandler:setMessage(MESSAGE_GREET, "Merry Christmas |PLAYERNAME|. I'm Santa Claus. I got {present}s for good children.")

local node = keywordHandler:addKeyword({'present'}, StdModule.say, {npcHandler = npcHandler, onlyFocus = true, text = 'Were you good this year?'})
node:addChildKeywordNode(KeywordNode:new({'yes'}, santaNPC, true))
node:addChildKeywordNode(KeywordNode:new({'no'}, santaNPC, false))
npcHandler:addModule(FocusModule:new())
npcHandler:addModule(VoiceModule:new(voices))
 
Hello, this is my first post, so, sorry if in wrong area. I have a santa script as well, and I'm wanting to give it a timer so that every day he will give another item. So far I've been messing around with the timing for quite a while and searching for answers, also integrating "A Sweaty Cyclops.lua" amulet part, and so far I've come to this, I feel like I'm so close lol(could be ages away :/). I've got enough 'OT Scripting' knowledge to do quite a bit, but this is the first timing stuff I've attempted. (It's set to 5 seconds for testing).

The error at the moment is he will give another item immediately after the first. Also tested with god and normal character, no errors in console. TFS 1.2

Script so far:

local random_items = {
{chance = 5, itemid = 2112}, -- 0.5% to get teddy bear
{chance = 20, itemid = 6512}, -- 2% to get santa doll
{chance = 40, itemid = 2114}, -- 4% to get piggy bank
{chance = 40, itemid = 2152, count = 100}, -- 4% to get 100 platinum coins
{chance = 80, itemid = 2111, count = 5}, -- 8% to get 5 snowballs
{chance = 80, itemid = 2688, count = 8}, -- 8% to get 8 candy canes
{chance = 80, itemid = 2110, count = 1}, -- 8% to get doll
{chance = 400, itemid = 2674, count = 15}, -- 40% to get 15 red apples
{chance = 450, itemid = 2675, count = 10}, -- 45% to get 10 oranges
{chance = 1000, itemid = 2687, count = 8} -- 100% to get 8 cookies
}

local PRESENT_STORAGE = 54163 -- storage ID
local PRESENT_TIMER = 54164

local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)


function onCreatureAppear(cid) npcHandler:eek:nCreatureAppear(cid) end
function onCreatureDisappear(cid) npcHandler:eek:nCreatureDisappear(cid) end
function onCreatureSay(cid, type, msg) npcHandler:eek:nCreatureSay(cid, type, msg) end
function onThink() npcHandler:eek:nThink() end

local voices = {
{ text = 'HO HO HO! MERRY CHRISTMAS', yell = true },
{ text = 'Hi there young ones, have you been good this year?' }
}

function santaNPC(cid, message, keywords, present, node)
if not npcHandler:isFocused(cid) then
return false
end
if not present then
npcHandler:say('Come back when you start behaving good.', cid)
return true
end
local player = Player(cid)
if player:getStorageValue(PRESENT_STORAGE) == 1 then
npcHandler:say('You\'ve already received a present |PLAYERNAME|.', cid)
elseif player:getStorageValue(PRESENT_TIMER) + 5 * 1000 > os.time() then
player:setStorageValue(PRESENT_STORAGE, -1)
end
local player = Player(cid)
local item, reward = nil, {}
for i = 1, #random_items do
item = random_items
if math.random(0, 1000) <= item.chance then
reward.itemid = item.itemid
reward.subType = item.count or 1
break
end
end
player:addItem(reward.itemid, reward.subType)
player:setStorageValue(PRESENT_STORAGE, 1)
player:setStorageValue(PRESENT_TIMER, os.time())
npcHandler:say(string.format('HO HO HO! You were good like a little dwarf this year! I got %d %s for you.', reward.subType, ItemType(reward.itemid):getName()), cid)
npcHandler:resetNpc()
return true
end

npcHandler:setMessage(MESSAGE_GREET, "Merry Christmas |PLAYERNAME|. I'm Santa Claus. I got {present}s for good children.")

local node = keywordHandler:addKeyword({'present'}, StdModule.say, {npcHandler = npcHandler, onlyFocus = true, text = 'Were you good this year?'})
node:addChildKeywordNode(KeywordNode:new({'yes'}, santaNPC, true))
node:addChildKeywordNode(KeywordNode:new({'no'}, santaNPC, false))
npcHandler:addModule(FocusModule:new())
npcHandler:addModule(VoiceModule:new(voices))
set a storage value for the timer to be os.time() + (24 * 60 * 60)
then use an if statement to compare that with current time
if storage <= os.time() then
givepresent
end
 
Hey man, thank you for the reply. To be honest the way you explained the solution is a lil' confusing to me lol, although to you and other scripting veterans would be simple, but this is the was I interpreted it.

local PRESENT_STORAGE = os.time() + (24 * 60 * 60)

function santaNPC(cid, message, keywords, present, node)
if not npcHandler:isFocused(cid) then
return false
end
if not present then
npcHandler:say('Come back when you start behaving good.', cid)
return true
end
local player = Player(cid)
local item, reward = nil, {}
for i = 1, #random_items do
item = random_items
if math.random(0, 1000) <= item.chance then
reward.itemid = item.itemid
reward.subType = item.count or 1
break
end
end
if player:getStorageValue(PRESENT_STORAGE) <= os.time then
player:addItem(reward.itemid, reward.subType)
npcHandler:say(string.format('HO HO HO! You were good like a little dwarf this year! I got %d %s for you.', reward.subType, ItemType(reward.itemid):getName()), cid)
npcHandler:resetNpc()
return true
else
npcHandler:say('You\'ve already received a present |PLAYERNAME|.', cid)
end
end

But the error is (on line 49: if player:getStorageValue(PRESENT_STORAGE) <= os.time then)
and says: attempt to compare number with function stack traceback
also wont give item, he says everything up until that, just stops there

Sorry for my nubness lol
 
Hey man, thank you for the reply. To be honest the way you explained the solution is a lil' confusing to me lol, although to you and other scripting veterans would be simple, but this is the was I interpreted it.

local PRESENT_STORAGE = os.time() + (24 * 60 * 60)

function santaNPC(cid, message, keywords, present, node)
if not npcHandler:isFocused(cid) then
return false
end
if not present then
npcHandler:say('Come back when you start behaving good.', cid)
return true
end
local player = Player(cid)
local item, reward = nil, {}
for i = 1, #random_items do
item = random_items
if math.random(0, 1000) <= item.chance then
reward.itemid = item.itemid
reward.subType = item.count or 1
break
end
end
if player:getStorageValue(PRESENT_STORAGE) <= os.time then
player:addItem(reward.itemid, reward.subType)
npcHandler:say(string.format('HO HO HO! You were good like a little dwarf this year! I got %d %s for you.', reward.subType, ItemType(reward.itemid):getName()), cid)
npcHandler:resetNpc()
return true
else
npcHandler:say('You\'ve already received a present |PLAYERNAME|.', cid)
end
end

But the error is (on line 49: if player:getStorageValue(PRESENT_STORAGE) <= os.time then)
and says: attempt to compare number with function stack traceback
also wont give item, he says everything up until that, just stops there

Sorry for my nubness lol
the present storage should be a constant
+ you shouldn't store the value outside of the function, it will only have 1 value since it only gets evaluated when you load the script
instead you want to get the value each time the function is executed
Code:
local PRESENT_STORAGE = 88888

function santaNPC(cid, message, keywords, present, node)
    if not npcHandler:isFocused(cid) then
        return false
    end
    if not present then
        npcHandler:say('Come back when you start behaving good.', cid)
        return true
    end
    local player = Player(cid)
    local item, reward = nil, {}
    for i = 1, #random_items do
        item = random_items
        if math.random(1000) <= item.chance then
            reward.itemid  = item.itemid
            reward.subType = item.count or 1
            break
        end
    end
    if player:getStorageValue(PRESENT_STORAGE) <= os.time() then
        player:addItem(reward.itemid, reward.subType)
        npcHandler:say(string.format('HO HO HO! You were good like a little dwarf this year! I got %d %s for you.', reward.subType, ItemType(reward.itemid):getName()), cid)
        npcHandler:resetNpc()
        player:setStorageValue(PRESENT_STORAGE, os.time() + (24 * 60 * 60)) -- sets the next time for 1 day later than current time
        return true
    else
        npcHandler:say('You\'ve already received a present |PLAYERNAME|.', cid)
    end
end
 
Ok so I moved the storage into the function and changed the if statement to what you had put, and he now gives present, but after time he still keeps saying: You\'ve already received a present |PLAYERNAME|.

Also os.time() + (5 * 60) is 5 seconds right? and should work for testing?

This is what I've got this so far:

function santaNPC(cid, message, keywords, present, node)
if not npcHandler:isFocused(cid) then
return false
end
if not present then
npcHandler:say('Come back when you start behaving good.', cid)
return true
end
local item, reward = nil, {}
for i = 1, #random_items do
item = random_items
if math.random(0, 1000) <= item.chance then
reward.itemid = item.itemid
reward.subType = item.count or 1
break
end
end
local player = Player(cid)
local PRESENT_STORAGE = 54163
if player:getStorageValue(PRESENT_STORAGE) <= os.time() then
player:addItem(reward.itemid, reward.subType)
npcHandler:say(string.format('HO HO HO! You were good like a little dwarf this year! I got %d %s for you.', reward.subType, ItemType(reward.itemid):getName()), cid)
npcHandler:resetNpc()
player:setStorageValue(PRESENT_STORAGE, os.time() + (5 * 60))
return true
else
npcHandler:say('You\'ve already received a present |PLAYERNAME|.', cid)
end
end

EDIT: Don't worry lmfao, told you I was nub, 5 * 60 is 5 mins, it works perfectly now
This is full function:

function santaNPC(cid, message, keywords, present, node)
if not npcHandler:isFocused(cid) then
return false
end
if not present then
npcHandler:say('Come back when you start behaving good.', cid)
return true
end
local item, reward = nil, {}
for i = 1, #random_items do
item = random_items
if math.random(0, 1000) <= item.chance then
reward.itemid = item.itemid
reward.subType = item.count or 1
break
end
end
local player = Player(cid)
local PRESENT_STORAGE = 54163
if player:getStorageValue(PRESENT_STORAGE) <= os.time() then
player:addItem(reward.itemid, reward.subType)
npcHandler:say(string.format('HO HO HO! You were good like a little dwarf this year! I got %d %s for you.', reward.subType, ItemType(reward.itemid):getName()), cid)
npcHandler:resetNpc()
player:setStorageValue(PRESENT_STORAGE, os.time() + 24 * 60 * 60)
return true
else
npcHandler:say('You\'ve already received a present |PLAYERNAME|.', cid)
end
end
 
Last edited:
Okay guys , Christmas is coming and for sure too many will need this script this one wasn't working with 1.2 or 1.3 i made some edits and its not working 100% in tfs 1.3
Merry Christmas,
Lua:
local random_items = {
{chance = 5, itemid = 2112}, -- 0.5% to get teddy bear
{chance = 20, itemid = 6512}, -- 2% to get santa doll
{chance = 40, itemid = 2114}, -- 4% to get piggy bank
{chance = 40, itemid = 2152, count = 100}, -- 4% to get 100 platinum coins
{chance = 80, itemid = 2111, count = 5}, -- 8% to get 5 snowballs
{chance = 80, itemid = 2688, count = 8}, -- 8% to get 8 candy canes
{chance = 80, itemid = 2110, count = 1}, -- 8% to get doll
{chance = 400, itemid = 2674, count = 15}, -- 40% to get 15 red apples
{chance = 450, itemid = 2675, count = 10}, -- 45% to get 10 oranges
{chance = 1000, itemid = 2687, count = 8} -- 100% to get 8 cookies
}
local PRESENT_TIMER = 54164

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 voices = {
{ text = 'HO HO HO! MERRY CHRISTMAS', yell = true },
{ text = 'Hi there young ones, have you been good this year?' }
}

local PRESENT_STORAGE = 88888
function santaNPC(cid, message, keywords, present, node)
    if not npcHandler:isFocused(cid) then
        return false
    end
    if not present then
        npcHandler:say('Come back when you start behaving good.', cid)
        return true
    end
    local player = Player(cid)
    local item, reward = nil, {}
    for i = 1, #random_items do
        item = random_items
        if math.random(1000) < item[i].chance then
            reward.itemid  = item[i].itemid
            reward.subType = item[i].count or 1
            break
        end
    end
    if player:getStorageValue(PRESENT_STORAGE) <= os.time() then
        player:addItem(reward.itemid, reward.subType)
        npcHandler:say(string.format('HO HO HO! You were good like a little dwarf this year! I got %d %s for you.', reward.subType, ItemType(reward.itemid):getName()), cid)
        npcHandler:resetNpc()
        player:setStorageValue(PRESENT_STORAGE, os.time() + (24 * 60 * 60)) -- sets the next time for 1 day later than current time
        return true
    else
        npcHandler:say('You\'ve already received a present.', cid)
    end
end

npcHandler:setMessage(MESSAGE_GREET, "Merry Christmas |PLAYERNAME|. I'm Santa Claus. I got {present}s for good children.")
local node = keywordHandler:addKeyword({'present'}, StdModule.say, {npcHandler = npcHandler, onlyFocus = true, text = 'Were you good this year?'})
node:addChildKeywordNode(KeywordNode:new({'yes'}, santaNPC, true))
node:addChildKeywordNode(KeywordNode:new({'no'}, santaNPC, false))
npcHandler:addModule(FocusModule:new())
 
Back
Top