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

NPC Summon (small issue, drives me crazy)

Prestin32k

New Member
Joined
Jul 16, 2012
Messages
20
Reaction score
0
Hi guys, I need youre help. I created a NPC summoner, who spawns monsters on demand.
There's a delay of 60 seconds between each reqeusts, so once in 60 seconds players can ask for monsters.
I use os.difftime for this, but the problem is, that I believe os.difftime is called system wide, so when I place multiple NPC's they trigger eachothers timer. Making it 60 seconds delay for _EVERY_ NPC when someone asks for monsters one time.

Code:
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 lastSound = 0
function onThink()
    if lastSound < os.time() then
        lastSound = (os.time() + 5)
        if math.random(100) < 15  then
            Npc():say("Keywords: rats, rotworm, cyclops, orc, dragons. Becareful!", TALKTYPE_SAY)
        end
    end
    npcHandler:onThink()
end

local function greetCallback(cid)
    local player = Player(cid)
        npcHandler:setMessage(MESSAGE_GREET, 'Hi |PLAYERNAME|, what do you wish me to summon? Try rats, rotworm, cyclops, orc, dragons')
    return true
end

local function creatureSayCallback(cid, type, msg)
    if not npcHandler:isFocused(cid) then
        return false
    end

    local rats = { 'Rat', 'Rat', 'Rat', 'Rat' }
    local rots = { 'Rotworm', 'Carrion Worm', 'Rotworm', 'Rotworm' }
    local cycs = { 'Cyclops', 'Cyclops Smith', 'Cyclops' }
    local orcs = { 'Orc Berserker', 'Orc Berserker', 'Orc Leader' }
    local dragons = { 'Dragon', 'Dragon Hatchling' }

    t1 = os.time()
    local time = os.difftime(t1, t2)
    local player = Player(cid)
        if time >= 60 then           
            if msgcontains(msg, "rat") then
                for i = 1, #rats do
                    Game.createMonster(rats[i], Npc():getPosition())
                    print(time)
                end   
                t2 = os.time()   
            end   
            if msgcontains(msg, "rotworm") then
                for i = 1, #rots do
                    Game.createMonster(rots[i], Npc():getPosition())
                    print(time)
                end   
                t2 = os.time()   
            end   
            if msgcontains(msg, "cyclops") then
                for i = 1, #cycs do
                    Game.createMonster(cycs[i], Npc():getPosition())
                    print(time)
                end   
                t2 = os.time()   
            end
            if msgcontains(msg, "orc") then
                for i = 1, #orcs do
                    Game.createMonster(orcs[i], Npc():getPosition())
                    print(time)
                end   
                t2 = os.time()   
            end
            if msgcontains(msg, "dragon") then
                for i = 1, #dragons do
                    Game.createMonster(dragons[i], Npc():getPosition())
                    print(time)
                end   
                t2 = os.time()   
            end       
        end
        Npc():say(60 - time .. " seconds left for a new spawn", TALKTYPE_SAY)
    return true
end


npcHandler:setCallback(CALLBACK_GREET, greetCallback)
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())

Would be great if someone helped me out! :)
The goal is: Let each NPC summon individually with their own timer.

TFS 1.1
PrinterLua RL-PACK
 
Code:
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 lastSound = 0
function onThink()
    if lastSound < os.time() then
        lastSound = (os.time() + 5)
        if math.random(100) < 15  then
            Npc():say("Keywords: rats, rotworm, cyclops, orc, dragons. Becareful!", TALKTYPE_SAY)
        end
    end
    npcHandler:onThink()
end

local config = {
    ['rat'] = {'Rat', 'Rat', 'Rat', 'Rat'},
    ['rotworm'] = {'Rotworm', 'Carrion Worm', 'Rotworm', 'Rotworm'},
    ['cyclops'] = {'Cyclops', 'Cyclops Smith', 'Cyclops'},
    ['orc'] = {'Orc Berserker', 'Orc Berserker', 'Orc Leader'},
    ['dragon'] = {'Dragon', 'Dragon Hatchling'}
}

local function creatureSayCallback(cid, type, msg)
    if not npcHandler:isFocused(cid) then
        return false
    end
   
    local player = Player(cid)
    for message, monsterName in pairs(config) do
        if msgcontains(msg, message) then
            local timeStorage = player:getStorageValue(1000)
            if timeStorage < os.time() then
                for i = 1, #monsterName do
                    Game.createMonster(monsterName[i], Npc():getPosition())
                end
                player:setStorageValue(1000, os.time() + 60)
            else
                Npc():say(timeStorage - os.time() .. " seconds left for a new spawn", TALKTYPE_SAY, false, player)
            end
        end
    end
    return true
end

npcHandler:setMessage(MESSAGE_GREET, 'Hi |PLAYERNAME|, what do you wish me to summon? Try rats, rotworm, cyclops, orc, dragons')

npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())
 
Last edited:
Code:
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 lastSound = 0
function onThink()
    if lastSound < os.time() then
        lastSound = (os.time() + 5)
        if math.random(100) < 15  then
            Npc():say("Keywords: rats, rotworm, cyclops, orc, dragons. Becareful!", TALKTYPE_SAY)
        end
    end
    npcHandler:onThink()
end

local config = {
    ['rat'] = {'Rat', 'Rat', 'Rat', 'Rat'},
    ['rotworm'] = {'Rotworm', 'Carrion Worm', 'Rotworm', 'Rotworm'},
    ['cyclops'] = {'Cyclops', 'Cyclops Smith', 'Cyclops'},
    ['orc'] = {'Orc Berserker', 'Orc Berserker', 'Orc Leader'},
    ['dragon'] = {'Dragon', 'Dragon Hatchling'}
}

local function creatureSayCallback(cid, type, msg)
    if not npcHandler:isFocused(cid) then
        return false
    end

    for message, monsterName in pairs(config) do
        if msgcontains(msg, message) then
            local cStorage = Game.getStorageValue(1000)
            if not cStorage or cStorage < os.time() then
                for i = 1, #monsterName do
                    Game.createMonster(monsterName[i], Npc():getPosition())
                end
                Game.setStorageValue(1000, os.time() + 60)
            else
                Npc():say(cStorage - os.time() .. " seconds left for a new spawn", TALKTYPE_SAY)
            end
        end
    end
    return true
end

npcHandler:setMessage(MESSAGE_GREET, 'Hi |PLAYERNAME|, what do you wish me to summon? Try rats, rotworm, cyclops, orc, dragons')

npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())

The problem still exists.. :(

When player X goes to NPC1, NPC1 summons correctly.
Then player X goes to NPC2, NPC2's timer is already triggered. (thats fine actually)
Within 60 seconds player Y goes to NPC1 or NPC2, and the timer is still counting from player X.

The goal is to let individual players talk to the NPC and summon according to their own 'timer'.

Appreciate the time and effort, I'll be looking for a small donation if solved :)
 
The problem still exists.. :(

When player X goes to NPC1, NPC1 summons correctly.
Then player X goes to NPC2, NPC2's timer is already triggered. (thats fine actually)
Within 60 seconds player Y goes to NPC1 or NPC2, and the timer is still counting from player X.

The goal is to let individual players talk to the NPC and summon according to their own 'timer'.

Appreciate the time and effort, I'll be looking for a small donation if solved :)


He told you how to fix it, gave you a scrip that would work depending on the players storage value, but only for that player, so it's just the same as an exhaustion for that player only, not for every player, and not for every npc... re-read the code?
 
He told you how to fix it, gave you a scrip that would work depending on the players storage value, but only for that player, so it's just the same as an exhaustion for that player only, not for every player, and not for every npc... re-read the code?

It is fixed already.
Thanks though for your reply.
 
Back
Top