• 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 help with os.time()

silveralol

Advanced OT User
Joined
Mar 16, 2010
Messages
1,480
Solutions
9
Reaction score
212
hello folks, I'm working on something that uses timer
I set storage to the player
Code:
player:setStorageValue(key, os.time() + 20 * 60 * 1000)
so, I want the time left
Code:
local timeLeft = player:getStorageValue(key) - os.time()
and send it to player
something like:
Code:
player:sendTextMessage('Time Left: 13 minutes and 20 seconds' , MESSAGE_EVENT_ADVANCE)
or
player:sendTextMessage('Time Left: 30 seconds' , MESSAGE_EVENT_ADVANCE)
thanks
 
I guess that you want to make a translator that gets as input raw seconds and transform to time text. Right?

You will need to make a function that get the seconds and tranform into minutes and hours, returning the string.

Lua:
function transformTime(seconds)
      -- declare string
      -- make math and append to string
      -- return string
end
 
I guess that you want to make a translator that gets as input raw seconds and transform to time text. Right?

You will need to make a function that get the seconds and tranform into minutes and hours, returning the string.

function transformTime(seconds)
-- declare string
-- make math and append to string
-- return string
end​
yes, I found some function that do it, but don't know how use it properly
Code:
 local function getHours(seconds)
    return math.floor((seconds/60)/60)
end

local function getMinutes(seconds)
    return math.floor(seconds/60)
end

local function getTime(seconds)
    local hours, minutes = getHours(seconds), getMinutes(seconds)
    if (minutes > 59) then
        minutes = minutes-hours*60
    end

    if (minutes < 10) then
        minutes = "0" ..minutes
    end

    return hours..":"..minutes.. "h"
end
this functions convert hours minutes, but not seconds
 
thank you, its works...
but I edit the main function, and get something weird
Code:
function showTimeLeft(number, usewords)
    local number = tonumber(number)
    if not number then
        return "error"
    end

    if number < 0 then
        return "expired"
    end

    local clocknum = os.date("!%X",number):split(":") -- h:m:s
    local day = math.modf(number / 86400)
    local hour = clocknum[1]
    local minute = clocknum[2]
    local second = clocknum[3]

    if not usewords then
        return table.concat({day, hour, minute, second}, ":")
    end

    local text = {}

    if minute ~= "00" then
        table.insert(text, tonumber(minute) .. " minute" .. (tonumber(minute) > 1 and "s" or ""))
    end

    if second ~= "00" then
        table.insert(text, tonumber(second) .. " second" .. (tonumber(second) > 1 and "s" or ""))
    end

    countdown_text = ""
    if #text > 0 then
        countdown_text = text[1]
        for i = 2, #text - 1 do
            countdown_text = countdown_text .. ", " .. text[i]
        end
        if #text > 1 then
            countdown_text = countdown_text .. " and " .. text[#text]
        end
        countdown_text = countdown_text .. " left."
    else
        countdown_text = "expired"
    end
    return countdown_text
end
I just remove day and hour..
then using my "idea" I set the timer by using
Code:
player:setStorageValue(key, os.time() + 1 * 60 * 1000)
for test, but the timer show 40 minutes, instead 1 minute less...
 
You dont need to remove day and hour, if time is short enough it will show just minutes and seconds.
 
You dont need to remove day and hour, if time is short enough it will show just minutes and seconds.
if I don't remove it it should alots of days
look:
Code:
16 hours, 39 minutes and 53 seconds left
when I use:
Code:
player:setStorageValue(key, os.time() + 1 * 60 * 1000)
and get the timer by using:
Code:
player:sendTextMessage('Time Left: ' ..showTimeLeft(timeLeft, true))
 
Back
Top