• 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 stop addEvent if position changes

Lava Titan

Developer
Joined
Jul 25, 2009
Messages
1,529
Solutions
1
Reaction score
85
Location
Portugal
Hi, I'm having an issue in stopping addEvent if I move:

Lua:
local function countdown(number, position, fromPosition, thingPos, msgx)
        local count = number
        for time = 1, number do
            if getThingPos(cid).x == 998 and getThingPos(cid).y == 995 and getThingPos(cid).z == 5 then
                addEvent(doSendAnimatedText, time * 1000, thingPos, count > 1 and count.."" or msgx .."", count < 30 and TEXTCOLOR_RED or TEXTCOLOR_GREEN)
                count = count -1
            else
                doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "You moved, timer was canceled.")
                doSendMagicEffect(getThingPos(cid), CONST_ME_POFF)
            end
        end
        count = number
    end

This is a simple countdown function, it's triggered by stepIn on a tile, now my issue is that if I change position the countdown continues..

Lua:
function onStepIn(cid, item, position, lastPosition, fromPosition, toPosition)
     countdown(timeInside, position, fromPosition, getThingPos(cid), "Timeout!")
return true
end

I also tried with storages but it didn't work :(
 
Last edited:
Solution
try this..
Lua:
local rooms = {
    {x = 998, y = 995, z = 5},
    {x = 1002, y = 995, z = 5}
}
local timeInside = 60
local lotteryStorage = 11062
local teleportOut = {x = 1006, y = 998, z = 5}

local function countdown(cid, count, text_pos)
    if not isPlayer(cid) then
        return false
    end
    if getCreatureStorage(cid, lotteryStorage) ~= 1 then
        return false
    end
    if count > 29 then
        doSendAnimatedText(text_pos, "" .. count .. "", TEXTCOLOR_GREEN)
    elseif count > 1 then
        doSendAnimatedText(text_pos, "" .. count .. "", TEXTCOLOR_RED)
    else
        doSendAnimatedText(text_pos, "Timeout!", TEXTCOLOR_RED)
        doTeleportThing(cid, teleportOut, false)
        return true
    end...
Maybe something like this
Lua:
local event = {}

local function count(cid, i, pos)
   if not isPlayer(cid) then
       return
   end
  
   doSendAnimatedText(pos, i == 0 and "Timeout!" or i, i < 30 and TEXTCOLOR_RED or TEXTCOLOR_GREEN)
   if i > 0 then
       event[cid] = addEvent(count, 1000, cid, i - 1, pos)
   end
end

function onStepIn(cid, item, topos, frompos)
   return true, count(cid, 60, topos)
end

function onStepOut(cid, item, topos, frompos)
   if stopEvent(event[cid]) then
       doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "You moved, timer was canceled.")
       doSendMagicEffect(getThingPos(cid), CONST_ME_POFF)
   end  
   return true
end
 
Thanks it kinda works, my issue is that the stepIn item is a teleport, so when I enter the teleport, I get teleported to a room, the timer starts and when I teleport out of the room the timer keeps going on inside the room
 
Thanks it kinda works, my issue is that the stepIn item is a teleport, so when I enter the teleport, I get teleported to a room, the timer starts and when I teleport out of the room the timer keeps going on inside the room
You didn't write about what the script is supposed to be doing, but it looks like 'king of the hill' type thing?

Check this out if that's your intent.
Xikini's Free Scripting Service. [0.3.7 & (0.3.6/0.4)]
 
ok i managed to finish the script, only issue still stopping the countdown loop xd

here's the full script:


Lua:
local rooms = {
    {x = 998, y = 995, z = 5},{x = 1002, y = 995, z = 5}
}
local timeInside = 60
local lotteryStorage = 11062
local teleportOut = {x = 1006, y = 998, z = 5}

local function doTeleportBack(cid, position, msg)
    if not isPlayer(cid) then
        return false
    end
        doTeleportThing(cid, position, false)
        doSendMagicEffect(position, CONST_ME_TELEPORT)
return true
end

local function countdown(number, position, fromPosition, thingPos, msgx)
        local count = number
        for time = 1, number do
            addEvent(doSendAnimatedText, time * 1000, thingPos, count > 1 and count.."" or msgx .."", count < 30 and TEXTCOLOR_RED or TEXTCOLOR_GREEN)
            count = count -1
        end
        count = number
    end

function onStepIn(cid, item, position, lastPosition, fromPosition, toPosition)

    for _, pos in pairs(rooms) do
        if not isPlayer(getTopCreature(pos).uid) then
            if doTeleportThing(cid, pos) then
                doCreatureSetStorage(cid, lotteryStorage, 1)
                countdown(timeInside, position, fromPosition, getThingPos(cid), "Timeout!")
                doCreatureSay(cid, "Good Luck!", TALKTYPE_MONSTER)
                doSendMagicEffect(getThingPos(cid), CONST_ME_TELEPORT)
                addEvent(doTeleportBack, (timeInside * 1000) + 1000, cid, teleportOut)
                return true
            end
        end
    end
    -- doPlayerPushBack(cid, position, fromPosition, showMSG, showCancel, creatureMsg, cancelMsg)
    doPlayerPushBack(cid, position, fromPosition, false, true, "creatureMsg", "There are no empty room's at this moment.") -- custom function in 050-functions.lua
return true
end
 
Try this.
Configure positions in config, also length in x and y at the line containing "getSpectators"
Lua:
local config = {
    pos = {
        centerPos = {x = 998, y = 995, z = 5}, -- Center of room
        gotoPos = {x = 1006, y = 998, z = 5},  -- Teleport player to
        exitPos = x = 1006, y = 998, z = 5}    -- Exit player to pos
    },
 
    event = {},
    duration = 60,
    storage = 11062,
}

local function operation(cid, i, slavePos)
    if not isPlayer(cid) then
        return
    end
 
    local pos = getThingPos(cid)
    if pos.x ~= slavePos.x or pos.y ~= slavePos.y or pos.z ~= slavePos.z then
        if config.event[cid] then
            -- Teleport back player?
            -- doTeleportThing(cid, config.pos.exitPos)
            return stopEvent(config.event[cid])
        end
    end
 
    doSendAnimatedText(pos, i == 0 and "Timeout!" or i, i < 30 and TEXTCOLOR_RED or TEXTCOLOR_GREEN)
    if i > 0 then
        config.event[cid] = addEvent(operation, 1000, cid, i - 1, slavePos)
    else
        doTeleportThing(cid, config.pos.exitPos)
        doSendMagicEffect(config.pos.exitPos, CONST_ME_TELEPORT)
    end
end

function onStepIn(cid, item, topos, frompos)
    local spectators = getSpectators(config.pos.centerPos, 5, 5)
    if not spectators then
        return true
    end
 
    local continue = true
    for _, pid in ipairs(spectators) do
        if isPlayer(getTopCreature(pid)) then
            continue = false
        end  
    end
 
    if continue and doTeleportThing(cid, config.pos.gotoPos) then
        local ppos = getThingPos(cid)
        doCreatureSetStorage(cid, config.storage, 1)
        operation(cid, config.duration, config.pos.gotoPos)
        doCreatureSay(cid, "Good Luck!", TALKTYPE_MONSTER)
        doSendMagicEffect(ppos, CONST_ME_TELEPORT)
    else
        doPlayerPushBack(cid, position, fromPosition, false, true, "creatureMsg", "There are no empty room's at this moment.")
    end
    return true
end
 
Last edited:
try this..
Lua:
local rooms = {
    {x = 998, y = 995, z = 5},
    {x = 1002, y = 995, z = 5}
}
local timeInside = 60
local lotteryStorage = 11062
local teleportOut = {x = 1006, y = 998, z = 5}

local function countdown(cid, count, text_pos)
    if not isPlayer(cid) then
        return false
    end
    if getCreatureStorage(cid, lotteryStorage) ~= 1 then
        return false
    end
    if count > 29 then
        doSendAnimatedText(text_pos, "" .. count .. "", TEXTCOLOR_GREEN)
    elseif count > 1 then
        doSendAnimatedText(text_pos, "" .. count .. "", TEXTCOLOR_RED)
    else
        doSendAnimatedText(text_pos, "Timeout!", TEXTCOLOR_RED)
        doTeleportThing(cid, teleportOut, false)
        return true
    end
    addEvent(countdown, 1000, cid, count - 1, text_pos)
end

function onStepIn(cid, item, position, lastPosition, fromPosition, toPosition)
    for _, pos in pairs(rooms) do
        if not isPlayer(getTopCreature(pos).uid) then
            if doTeleportThing(cid, pos) then
                doCreatureSetStorage(cid, lotteryStorage, 1)
                addEvent(countdown, 500, cid, timeInside, pos)
                doCreatureSay(cid, "Good Luck!", TALKTYPE_MONSTER)
                doSendMagicEffect(pos, CONST_ME_TELEPORT)
                return true
            end
        end
    end
    doPlayerPushBack(cid, position, fromPosition, false, true, "creatureMsg", "There are no empty room's at this moment.") -- custom function in 050-functions.lua
    return true
end

function onStepOut(cid, item, position, fromPosition)
    doCreatureSetStorage(cid, lotteryStorage, 0)
    return true
end
 
Solution
wow it works, can you tell me how does stepOut work here? like I assume that stepOut is based on the tile where I'm standing, but in this case the tile where I stepIn is not the same tile thats why I question how come stepOut works xd
 
wow it works, can you tell me how does stepOut work here? like I assume that stepOut is based on the tile where I'm standing, but in this case the tile where I stepIn is not the same tile thats why I question how come stepOut works xd
Uhhh. xD
I made a lot of assumptions based on the picture you sent me..
So it might not even be working correctly if what you said is correct.


--edit
Not sure why it was working.

We created a second onStepIn script that removed the storage instead.
 
Last edited:
Based on an issue @Xikini told me early, I made this script so if the player enters the teleport, by mistake, on purpose, w.e his items will be teleported out with him..

Lua:
function onStepIn(cid, item, position, lastPosition, fromPosition, toPosition)
    local tablePos = {x = 1000, y = 994, z = 6, stackpos = 255} -- pos where items are
    local outsidePos = {x = 1000, y = 998, z = 6} -- pos where player and items will be teleported too
    local thing = getThingFromPos(tablePos) -- checks if there is any item in the table pos
    local item_count = thing.type -- counts the amount of items in the table pos

    local function teleportOut()
        doTeleportThing(cid, outsidePos)
        doCreatureSetStorage(cid, 11062, 0)
    end

    local function teleportStuffOut(cid, count, pos)
            if count > 0 then
                    doTeleportThing(getThingFromPos(pos).uid, outsidePos, false)
                return true
            end
        addEvent(teleportStuffOut, 1, cid, count - 1, pos)
    end

    if item_count > 0 then -- checks if there are items on the table
        teleportOut() -- teleports out normally
        addEvent(teleportStuffOut, 1, cid, item_count, tablePos) -- teleports all the items out based on the amount of items in the table thing.type?
        doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "You left some items on the room, they were teleported under you, hurry up and pick them!")
    else -- no items were found on the table so player gets teleported out
        teleportOut()
    end
    return true
end

Now I'm kind of stuck because I get no errors and the script isn't working :( I assume it's related to stackpos?
 
Based on an issue @Xikini told me early, I made this script so if the player enters the teleport, by mistake, on purpose, w.e his items will be teleported out with him..

Lua:
function onStepIn(cid, item, position, lastPosition, fromPosition, toPosition)
    local tablePos = {x = 1000, y = 994, z = 6, stackpos = 255} -- pos where items are
    local outsidePos = {x = 1000, y = 998, z = 6} -- pos where player and items will be teleported too
    local thing = getThingFromPos(tablePos) -- checks if there is any item in the table pos
    local item_count = thing.type -- counts the amount of items in the table pos

    local function teleportOut()
        doTeleportThing(cid, outsidePos)
        doCreatureSetStorage(cid, 11062, 0)
    end

    local function teleportStuffOut(cid, count, pos)
            if count > 0 then
                    doTeleportThing(getThingFromPos(pos).uid, outsidePos, false)
                return true
            end
        addEvent(teleportStuffOut, 1, cid, count - 1, pos)
    end

    if item_count > 0 then -- checks if there are items on the table
        teleportOut() -- teleports out normally
        addEvent(teleportStuffOut, 1, cid, item_count, tablePos) -- teleports all the items out based on the amount of items in the table thing.type?
        doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "You left some items on the room, they were teleported under you, hurry up and pick them!")
    else -- no items were found on the table so player gets teleported out
        teleportOut()
    end
    return true
end

Now I'm kind of stuck because I get no errors and the script isn't working :( I assume it's related to stackpos?
Lua:
local item_count = thing.type -- counts the amount of items in the table pos
Yeah.. this only counts the amount of objects in a stack.. like 50 gold coins.

I'm kind of laughing a bit because your going to cry when you look at how short I'm going to make this code.

Lua:
local tablePos = {x = 1000, y = 994, z = 6}
local teleportPos = {x = 1000, y = 998, z = 6}

function onStepIn(cid, item, position, lastPosition, fromPosition, toPosition)
    doCreatureSetStorage(cid, 11062, 0)
    doTeleportThing(cid, teleportPos)
    doRelocate(tablePos, teleportPos, false, false)
    return true
end
 
Last edited:
Back
Top