• 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 [TFS 1.3] How to only display value if different than zero? And plural

  • Thread starter Thread starter Evil Puncker
  • Start date Start date
E

Evil Puncker

Guest
Hi everyone, currently the script bellow displays the following:

Elapsed time: 0 days, 0 hours, 13 minutes."

how do I make it not display the days and hours if they are zero? like this:

Elapsed time: 13 minutes."

and if you could add singular and plural (1 hours > 1 hour), even better 😁

LUA:
    local str = ""
    local secs = ""
    secs = 1554654 -- fictional value
    -- converting secs --
    local hours = math.ceil(secs / 3600) - 1
    local minutes = math.ceil((secs - (3600 * hours)) / 60)
    if (minutes == 60) then
        minutes = 0
        hours = hours + 1
    end
    local days = math.ceil(hours / 24) - 1
    hours = math.ceil(hours - (24 * days))
    if (hours == 24) then
        hours = 0
        days = days + 1
    end
    -- end of conversion --
    str = "Elapsed time: ".. days .." days, ".. hours .." hours, ".. minutes .." minutes."
    player:sendTextMessage(MESSAGE_STATUS_CONSOLE_ORANGE, str)
    return true
 
Solution
Use my small function
LUA:
format_time(seconds)
LUA:
function format_time(seconds)
    if seconds <= 0 then
        return '0s'
    end
    local days = math.floor(seconds / 86400)
    seconds = (seconds % 86400)
    local hours = math.floor(seconds / 3600)
    seconds = (seconds % 3600)
    local minutes = math.floor(seconds / 60)
    seconds = (seconds % 60)
    local formated = ''
    if days > 0 then
        formated = ("%s%ud"):format(formated, days)
    end
    if hours > 0 then
        if days > 0 then
            formated = formated .. ":"
        end
        formated = ("%s%uh"):format(formated, hours)
    end
    if minutes > 0 then
        if hours > 0 then
            formated = formated .. ":"
        end
        formated =...
Code:
local str = ""
local secs = ""
secs = 1554654 -- fictional value

-- converting secs --
local hours = math.ceil(secs / 3600) - 1

local minutes = math.ceil((secs - (3600 * hours)) / 60)

if (minutes == 60) then
    minutes = 0
    hours = hours + 1
end

local days = math.ceil(hours / 24) - 1
hours = math.ceil(hours - (24 * days))

if (hours == 24) then
    hours = 0
    days = days + 1
end

-- end of conversion --

str = "Elapsed time:"

if days > 0 then
    str = str .. " " .. days .. " " .. (days > 1 and "days" or "day")
    if hours > 0 or minutes > 0 then
        str = str .. ","
    end
end

if hours > 0 then
    str = str .. " " .. hours .. " " .. (hours > 1 and "hours" or "hour")
    if minutes > 0 then
        str = str .. ","
    end
end


if minutes > 0 then
    str = str .. " " .. minutes .. " " .. (minutes > 1 and "hours" or "minute")
end

str = str .. "."

player:sendTextMessage(MESSAGE_STATUS_CONSOLE_ORANGE, str)

return true
 
Last edited:
Use my small function
LUA:
format_time(seconds)
LUA:
function format_time(seconds)
    if seconds <= 0 then
        return '0s'
    end
    local days = math.floor(seconds / 86400)
    seconds = (seconds % 86400)
    local hours = math.floor(seconds / 3600)
    seconds = (seconds % 3600)
    local minutes = math.floor(seconds / 60)
    seconds = (seconds % 60)
    local formated = ''
    if days > 0 then
        formated = ("%s%ud"):format(formated, days)
    end
    if hours > 0 then
        if days > 0 then
            formated = formated .. ":"
        end
        formated = ("%s%uh"):format(formated, hours)
    end
    if minutes > 0 then
        if hours > 0 then
            formated = formated .. ":"
        end
        formated = ("%s%um"):format(formated, minutes)
    end
    if seconds > 0 then
        if minutes > 0 then
            formated = formated .. ":"
        end
        formated = ("%s%us"):format(formated, seconds)
    end
    return formated
end

example of use:
LUA:
print(format_time(25888888))
-- result: 299d:15h:21m:28s

print(format_time(357))
-- result: 5m:57s
 
Solution
Back
Top