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

TFS 1.X Counter starts when the player has a storage.

donabimbo

Member
Joined
May 1, 2023
Messages
27
Reaction score
9
GitHub
donabimbo
Greetings, I was thinking of making a counter by getting a storage.
When getting for example the storage 4000, start a counter with its days, hours, minutes and seconds.
This is an example: when saying !time, tell me the player has storage since 4 hours, 6 minutes and 17 seconds.
It is to know how long was the player with X storage. I need this for a quest task, that when the player accepts a mission a counter starts.
I only need the counter function.
 
You use Epoch Time
that is going to be the value to assign to the storage 4000

Lua:
-- Set epoch time value
local epochTime = os.time()

-- Add 6 hours to epoch time
local newEpochTime = epochTime + (6 * 60 * 60)

-- Check if current epoch time is after newEpochTime
if epochTime > newEpochTime then
print("Current time is after newEpochTime") -- 6 hours has expired, tell player they have failed quest and to go get it again
else
print("Current time is not after newEpochTime") -- quest is still active
end

epoch time is a representation of a date/time but can be stored as a single INT or STRING
So you use a timestamp for storage 4000, and then whenever you need to check or display a counter you can convert it back to a human readable date/time:

Lua:
-- Calculate the time difference
local currentTime = os.time()
local timeDifference = os.difftime(currentTime, epochTime)

-- Convert time difference to hours, minutes, and seconds
local hours = math.floor(timeDifference / 3600)
local minutes = math.floor((timeDifference % 3600) / 60)
local seconds = math.floor(timeDifference % 60)

-- Display the elapsed time
local elapsedTime = string.format("%d hour(s) %d minute(s) %d second(s)", hours, minutes, seconds)
print("Elapsed Time:", elapsedTime)
 
Last edited:
Example of it being used.
Lua:
local storageKey = 4000

local give_storage = TalkAction("/give_storage", "!give_storage")

function give_storage.onSay(player, words, param)
    -- give the storage
    player:setStorageValue(storageKey, os.time())
    player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "Your storage has been set as the current time.")
    return false
end

give_storage:separator(" ")
give_storage:register()

Lua:
local storageKey = 4000

local tell_time = TalkAction("/tell_time", "!tell_time")

function tell_time.onSay(player, words, param)
    -- get the storage you gave with other talkaction
    local epochTime = player:getStorageValue(storageKey)
 
    -- Calculate the time difference
    local currentTime = os.time()
    local timeDifference = os.difftime(currentTime, epochTime)
 
    -- Convert time difference to hours, minutes, and seconds
    local hours = math.floor(timeDifference / 3600)
    local minutes = math.floor((timeDifference % 3600) / 60)
    local seconds = math.floor(timeDifference % 60)
 
    -- Display the elapsed time
    local elapsedTime = string.format("%d hour(s) %d minute(s) %d second(s)", hours, minutes, seconds)
    player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "Elapsed Time: " .. elapsedTime)
    return false
end

tell_time:separator(" ")
tell_time:register()
 
Last edited:
Back
Top