Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How To Transform Seconds Into Hours/Minutes/Seconds?

Asked by 3 years ago

hey Devs I have been Working On A Daily Reward System And I Cant Transform the Seconds Into Hours/ Minutes / Seconds can you guys help me in it. my code is this

local Chest = game.Workspace.Chest

local Chest2 = game.Workspace.CHEST2

local Lower = Chest2.NameTag.Lowertext

local debounce = false

local drtime = 6*60*60

local dailyreward = 250

Lower.Text = dailyreward

game.Players.PlayerAdded:Connect(function(plr)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = plr

    local cash = Instance.new("IntValue")
    cash.Name = "Gold"
    cash.Value = 50
    cash.Parent = leaderstats

    Chest.Touched:Connect(function()
        if not debounce then
            debounce = true
            cash.Value = cash.Value + dailyreward
            for i = drtime, 1, -1 do
                Lower.Text = i
                wait(1)
            end
            debounce = false
            Lower.Text = dailyreward
        end
    end)
end) -- DONT MIND IT I KNOW ITS CRAPPY
0
it does doe works as i want it to work kidsteve923 139 — 3y
0
I don't understand what you want us to do Spjureeedd 385 — 3y
0
i Said I want the seconds to be in hours / minutes / seconds LIKE A TIMER on daily rewards kidsteve923 139 — 3y
0
Yes thank you, it really helped when you repeated the exact same thing. Do you wonder how many seconds there is in one minute and one hour or what exactly is it you want? Spjureeedd 385 — 3y

1 answer

Log in to vote
1
Answered by
sifn 70
3 years ago

This takes math and I am not particularly good at math, so I searched your question through Google to see if it was already solved by someone else in the past - chances are it was, because this is actually a common question - and good news friend, it was!

Right, so the solution can be found in this DevForum post. Have a look if you can. To summarize, all you need to do is copy-paste two functions into your code:

function Format(Int)
    return string.format("i", Int)
end

function convertToHMS(Seconds)
    local Minutes = (Seconds - Seconds%60)/60
    Seconds = Seconds - Minutes*60
    local Hours = (Minutes - Minutes%60)/60
    Minutes = Minutes - Hours*60

    return Format(Hours)..":"..Format(Minutes)..":"..Format(Seconds)
end

The two above functions can be used to format any given amount of seconds into the h:m:s format you're looking for. Put them at the top of your script.

With that done, simply format the number of seconds remaining using the convertToHMS() function every time a second is subtracted. It would look like so:

for i = drtime, 1, -1 do
    Lower.Text = convertToHMS(i)
    wait(1)
end

Et voila. Your script now should count down from 21600 seconds, or 6 hours, in the appropriate format, making for the daily reward system you desire.

Be sure to mark my answer as the solution (with the green checkmark) if this works for you! If it doesn't work, let me know with a comment and I will see if there is something I missed. :)

Ad

Answer this question