• 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 onThink [TFS 1.1]

Lava Titan

Developer
Joined
Jul 25, 2009
Messages
1,571
Solutions
3
Reaction score
98
Location
Portugal
Hi, I made this globalevent script to send the storage timeleft but I guess the way I made it wont work as a globalevent, could someone help me fixing it?

Basicly I want to send text with timer to all players that got storage 999999...

Code:
function onThink(interval, lastExecution, thinkInterval)
    local players = Game.getPlayers()
    local uid = #players
    local jogador = players[uid]

    local H = os.date("%H", jogador:getStorageValue(999999) - os.time())
    local M = os.date("%M", jogador:getStorageValue(999999) - os.time())
    local S = os.date("%S", jogador:getStorageValue(999999) - os.time())

    if #players > 0 then
        if jogador:getStorageValue(999999) >= os.time() then
            jogador:sendTextMessage(MESSAGE_INFO_DESCR, "Time Left: ".. H ..":".. M ..":".. S ..".")
        end
    end

    return true
end
 
Last edited:
Code:
function onThink(interval, lastExecution, thinkInterval)
    for _, player in ipairs(Game.getPlayers()) do
        local timeStorage = player:getStorageValue(999999)
        if timeStorage >= os.time() then
            player:sendTextMessage(MESSAGE_INFO_DESCR, string.format("Time left: %s", os.date("%H:%M:%S", timeStorage - os.time())))
        end

    end
    return true
end
You should consider using a scheduled event (addEvent) instead of globalevent. :p
 
I tried to use addEvent on the main script but I have no idea how to make it send the message every x seconds xd

P.S: tested ur code and it works perfectly but i'd like if you could explain how to use addevent for this case
 
Local function:
Code:
local function timeLeftMessage(playerId)
    local player = Player(playerId)
    if not player then -- Stop the event if the player is offline
        return
    end

    local timeLeft = player:getStorageValue(999999) - os.time()
    if timeLeft < 0 then -- Stop the event if timeLeft is a negative value
        return
    end

    player:sendTextMessage(MESSAGE_INFO_DESCR, string.format("Time left: %s", os.date("!%X", timeLeft)))
    addEvent(timeLeftMessage, 10000, playerId)
end
Usage:
Code:
timeLeftMessage(playerId) or addEvent(timeLeftMessage, 10000, playerId)
 
console error
Code:
Lua Script Error: [CreatureScript Interface]
data/creaturescripts/scripts/login.lua:onLogin
LuaScriptInterface::luaAddEvent(). Argument #3 is unsafe
stack traceback:
        [C]: in function 'addEvent'
        data/lib/compat/compat.lua:1101: in function 'timeLeftMessage'
        data/creaturescripts/scripts/login.lua:25: in function <data/creaturescr
ipts/scripts/login.lua:1>

compat.lua
Code:
function timeLeftMessage(playerId, storage)
    local player = Player(playerId)
    if not player then -- Stop the event if the player is offline
        return
    end

    local timeLeft = player:getStorageValue(storage) - os.time()
    if timeLeft < 0 then -- Stop the event if timeLeft is a negative value
        return
    end

    player:sendTextMessage(MESSAGE_INFO_DESCR, string.format("Time left: %s", os.date("%H:%M:%S", timeLeft)))
    addEvent(timeLeftMessage, 1000, playerId)
end

login.lua
Code:
timeLeftMessage(player, 999999)
 
Back
Top