• 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!
  • If you're using Gesior 2012 or MyAAC, please review this thread for information about a serious security vulnerability and a fix.

TFS 1.X+ stopEvent wont execute

Mjmackan

Mapper ~ Writer
Premium User
Joined
Jul 18, 2009
Messages
1,393
Solutions
14
Reaction score
168
Location
Sweden
Started to create a npc and now tried it out, somehow when i answer correct (aka 22) the npc won't stop the addEvent, I printed the stopEvent and it said failed, but why?
TFS 1.4+

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

function greet(cid, type, msg)
local player = Player(cid)
local qStorage = 89001
local answerWrongStorage = 89002
   
if (not npcHandler:isFocused(cid)) then return false end

    if Game.getStorageValue(qStorage) == nil then
        Game.setStorageValue(qStorage, 0)
    end

    if Game.getStorageValue(answerWrongStorage) == nil then
        Game.setStorageValue(answerWrongStorage, 0)
    end
   
    firstEvent = {}

    local function firstSpawn(player)
        npcHandler:say("Suprise it was! Tell me {ready} when you killed the monsters, so we can continue.",cid)
        Game.setStorageValue(qStorage, 2)
        Game.createMonster("werebadger", {x = 1511, y = 1122, z = 7})
        Game.createMonster("werebadger", {x = 1509, y = 1125, z = 7})
     return true
    end
   
 if msgcontains(msg, "ready") and Game.getStorageValue(qStorage) < 1 then
    npcHandler:say("I will ask you 5 questions, you have 10 seconds to answer each question. If you don't answer or answer faulty.. you're up for a suprise!",cid)
    addEvent(function()
        Game.setStorageValue(answerWrongStorage, 1)
        npcHandler:say("First question! 3 + 19 = ",cid)
        Game.setStorageValue(qStorage, 1)
    end, 6000)
    firstEvent[player:getId()] = addEvent(firstSpawn, 16000, player:getId())
 end
   
    if msgcontains(msg, "22") and Game.getStorageValue(qStorage) == 1 then --succeeded question 1
            stopEvent(firstEvent[player:getId()])
            firstEvent[player:getId()] = nil
            npcHandler:say("Well done, maybe a bit too easy right?!",cid)
        addEvent(function()
            npcHandler:say("Second question! Whats the full name of the daily quest npc?",cid)
            Game.setStorageValue(answerWrongStorage, 1)
            Game.setStorageValue(qStorage, 2)
        end, 3500)
    local secondEvent = addEvent(function()
        npcHandler:say("Suprise here we gooo! Tell me {ready} when you killed the monsters, so we can continue.",cid)
        Game.setStorageValue(qStorage, 3)
        Game.createMonster("werebadger", {x = 1511, y = 1122, z = 7})
        Game.createMonster("werebadger", {x = 1509, y = 1125, z = 7})
        Game.createMonster("werebadger", {x = 1511, y = 1127, z = 7})
        Game.createMonster("werebadger", {x = 1515, y = 1126, z = 7})
    end, 13500)
    elseif not msgcontains(msg, "22") and Game.getStorageValue(qStorage) == 1 and Game.getStorageValue(answerWrongStorage) == 1 then
        stopEvent(firstEvent)
        npcHandler:say("Suprise it was! Tell me {ready} when you killed the monsters, so we can continue.",cid)
        Game.setStorageValue(qStorage, 2)
        Game.createMonster("werebadger", {x = 1511, y = 1122, z = 7})
        Game.createMonster("werebadger", {x = 1509, y = 1125, z = 7})
        return true
    end
   
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, greet)
npcHandler:addModule(FocusModule:new())
 
Solution
You also are overwritting firstEvent table on every message
If you say ready data is saved to table but once you say 22 all data is gone

Just declare firstEvent table outside the greet function, use local aswell
OP
OP
Mjmackan

Mjmackan

Mapper ~ Writer
Premium User
Joined
Jul 18, 2009
Messages
1,393
Solutions
14
Reaction score
168
Location
Sweden
you're trying to stop a table, not an event id
My first edition looked like this and didn't work neither:
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

function greet(cid, type, msg)
local player = Player(cid)
local qStorage = 89001
local answerWrongStorage = 89002
  
if (not npcHandler:isFocused(cid)) then return false end

    if Game.getStorageValue(qStorage) == nil then
        Game.setStorageValue(qStorage, 0)
    end

    if Game.getStorageValue(answerWrongStorage) == nil then
        Game.setStorageValue(answerWrongStorage, 0)
    end

    local function firstSpawn(player)
        npcHandler:say("Suprise it was! Tell me {ready} when you killed the monsters, so we can continue.",cid)
        Game.setStorageValue(qStorage, 2)
        Game.createMonster("werebadger", {x = 1511, y = 1122, z = 7})
        Game.createMonster("werebadger", {x = 1509, y = 1125, z = 7})
     return true
    end
  
 if msgcontains(msg, "ready") and Game.getStorageValue(qStorage) < 1 then
    npcHandler:say("I will ask you 5 questions, you have 10 seconds to answer each question. If you don't answer or answer faulty.. you're up for a suprise!",cid)
    addEvent(function()
        Game.setStorageValue(answerWrongStorage, 1)
        npcHandler:say("First question! 3 + 19 = ",cid)
        Game.setStorageValue(qStorage, 1)
    end, 6000)
    firstEvent = addEvent(firstSpawn, 16000, player:getId())
 end
  
    if msgcontains(msg, "22") and Game.getStorageValue(qStorage) == 1 then --succeeded question 1
            stopEvent(firstEvent)
            firstEvent[player:getId()] = nil
            npcHandler:say("Well done, maybe a bit too easy right?!",cid)
        addEvent(function()
            npcHandler:say("Second question! Whats the full name of the daily quest npc?",cid)
            Game.setStorageValue(answerWrongStorage, 1)
            Game.setStorageValue(qStorage, 2)
        end, 3500)
    local secondEvent = addEvent(function()
        npcHandler:say("Suprise here we gooo! Tell me {ready} when you killed the monsters, so we can continue.",cid)
        Game.setStorageValue(qStorage, 3)
        Game.createMonster("werebadger", {x = 1511, y = 1122, z = 7})
        Game.createMonster("werebadger", {x = 1509, y = 1125, z = 7})
        Game.createMonster("werebadger", {x = 1511, y = 1127, z = 7})
        Game.createMonster("werebadger", {x = 1515, y = 1126, z = 7})
    end, 13500)
    elseif not msgcontains(msg, "22") and Game.getStorageValue(qStorage) == 1 and Game.getStorageValue(answerWrongStorage) == 1 then
        stopEvent(firstEvent)
        npcHandler:say("Suprise it was! Tell me {ready} when you killed the monsters, so we can continue.",cid)
        Game.setStorageValue(qStorage, 2)
        Game.createMonster("werebadger", {x = 1511, y = 1122, z = 7})
        Game.createMonster("werebadger", {x = 1509, y = 1125, z = 7})
        return true
    end
  
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, greet)
npcHandler:addModule(FocusModule:new())
 

jestem pro

That is the question
Joined
Apr 20, 2013
Messages
650
Solutions
14
Reaction score
86
This tries to stop the table
Lua:
stopEvent(firstEvent)
Change it everywhere for this
Lua:
stopEvent(firstEvent[player:getId()])
            firstEvent[player:getId()] = nil
In the first statement and the second too
 

Roddet

Premium User
Support Team
Joined
May 1, 2013
Messages
807
Solutions
86
Reaction score
586
Location
Mex
You also are overwritting firstEvent table on every message
If you say ready data is saved to table but once you say 22 all data is gone

Just declare firstEvent table outside the greet function, use local aswell
 
Solution
OP
OP
Mjmackan

Mjmackan

Mapper ~ Writer
Premium User
Joined
Jul 18, 2009
Messages
1,393
Solutions
14
Reaction score
168
Location
Sweden
This tries to stop the table
Lua:
stopEvent(firstEvent)
Change it everywhere for this
Lua:
stopEvent(firstEvent[player:getId()])
            firstEvent[player:getId()] = nil
In the first statement and the second too
Thats how it was compiled last time but Roddet made the correct statement.
You also are overwritting firstEvent table on every message
If you say ready data is saved to table but once you say 22 all data is gone

Just declare firstEvent table outside the greet function, use local aswell
Thanks, the function got so large so I forgot it was a function even lol.
 
Top