• 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!
  • 2026 staff recruitment is open! Check it out and consider applying!

Lua addEvent Issue

Lava Titan

Developer
Joined
Jul 25, 2009
Messages
1,571
Solutions
3
Reaction score
98
Location
Portugal
Hey there, I created this script to give an item each 10 seconds the issue is that it's giving the item everytime you step on the tile, so you can just spam out and in to abuse, can some1 explain how can I fix it please?

Thanks <3

Code:
local function giveFreeItems(playerId, amount) -- giveFreeItems(player:getId(), 1)
    local player = Player(playerId)
    if not player then
        return
    end

    if player:getStorageValue(616161) == nil then
        return
    end

    if player:getStorageValue(616161) == 0 then
        return
    end

    if player:getStorageValue(616161) == 1 then
        player:addItem(2152, 1)
        player:sendTextMessage(MESSAGE_INFO_DESCR, " You received a platinum coin. ")
        addEvent(giveFreeItems, 10000, playerId)
    end
    return true
end

function onStepIn(creature, item, position, fromPosition)
    local player = creature:getPlayer()
    if player == nil or player:isInGhostMode() then
        return true
    end

    player:setStorageValue(616161, 1)
    giveFreeItems(player:getId(), 1)
    player:sendTextMessage(MESSAGE_INFO_DESCR, "INSIDE")
return true
end

function onStepOut(creature, item, position, fromPosition)
    local player = creature:getPlayer()
    if player == nil or player:isInGhostMode() then
        return true
    end

    player:setStorageValue(616161, 0)
    giveFreeItems(player:getId(), 1)
    player:sendTextMessage(MESSAGE_INFO_DESCR, "OUTSIDE")
return true
end
 
Code:
-- time_ is based on the total number of plat given,
-- since its 1 plat per addEvent execution for the duration of time_
local time_ = 10
local storage = 616161
local plat = 2152

function onStepIn(player, item, position, fromPosition)
    -- if it isn't a player on the tile or the player is in ghost mode do nothing
    if not player:isPlayer() or player:isInGhostMode() then
        return true
    end
 
    -- lets maker sure they haven't stepped on the tile already
    if player:getStorageValue(storage) < 1 then
        -- if they haven't lets set the storage value, so they only get the plat 10 times
        player:setStorageValue(storage, 1)
        for i = 1, time_ do
            addEvent(
                function(id) -- annonymous function with 1 parameter, id
                    local player = Player(id) -- the id passed
                    if player:isPlayer() then -- make sure the player exists
                        player:addItem(plat, 1) -- give them the money
                        player:sendTextMessage(MESSAGE_INFO_DESCR, " You received a platinum coin. ")
                    end
                end,
                i * 1000, -- time in seconds, whatever i is equal to times 1000 millisecond
                player:getId() -- player id which will be passed to the annonymous function as argument
            )
        end
    end
    return true
end

function onStepOut(player, item, position, fromPosition)
    return true
end
 
Last edited:
Thanks everyone! <3

@Codex NG


Glad to see you're back :D

So resuming your script, it will give 1 platinum each second for 10 seconds?

So if I set time = 1440000 (24 hours) it will give 1 platinum each 1 second for 24 hours?
 
Thanks everyone! <3

@Codex NG


Glad to see you're back :D

So resuming your script, it will give 1 platinum each second for 10 seconds?

So if I set time = 1440000 (24 hours) it will give 1 platinum each 1 second for 24 hours?
I don't know what 1440000 is but 24 hours is 60 * 60 * 1000 * 24 = 86400000
But that isn't what you should do.
 
I will read again, maybe I miss something thanks

btw can some1 tell me what's the function to get current online players in TFS 1.2?

do people use spectator function with infinite range and multi floor or smth like that?
 
Back
Top