How could I make my cooldown also display a tenth of a decimal?

Hey DevForum!

I’m currently working on a Battle Cats inspired game, and I added a countdown-esc cooldown UI to prevent spam when spawning units, as shown here.

I also wanted for my cooldown to display tenths of a decimal, but I used the “for i = etc.” for the number countdown, which doesn’t really pair well with decimals from what I know of.

Any way I could add tenths to the countdown? Thanks!

1 Like

Remember seeing this in a post

local start = tick() 
while true do
    wait()
 
    local diff = tick() - start
    print(diff, "seconds has passed since the start") 

    local min = math.floor(diff / 60)
    local sec = diff - min*60
    local mil = math.floor((sec - math.floor(sec)) * 10) -- doing 7.123 - 7 gives us 0.123, then multiply by 10 and floor to get just 1
    sec = math.floor(sec)
    print(min .. ":" .. sec .. ":" .. mil, "total time has passed") 
end
2 Likes

I played around with this script a bit and I got some stuff down, but I can’t seem to figure out how to make it count down instead of up. Any suggestions?

(And yes, I’m bad with math :coefficients:)

1 Like

Try

for i = start, end, -0.1 do
3 Likes

It seemed to work… at first. But the UI seems to go wack once its below 1.9

https://i.gyazo.com/5c89c17bc0eee907c387445f29072093.mp4

Thanks for the suggestion, though! :coefficients:

1 Like

Thats quite impressive on roblox’s part

At that point Id just do

for i = start * 10, finish * 10, -1 do
    text = i/10
end
3 Likes

Tysm!! Now it’s working as intended! :coefficients:

1 Like

What you’re encountering is a double floating point precision error.

Not enough bits to represent the decimal number you wish to represent.

1 Like

To add onto this, for the people who did wait(1)

for i = cd,0 do
	AbilityDisplay.MainFrame[m].CooldownDisplay.Text = "Cooldown: "..i
	wait(1)
end

for their cooldown that went down without decimals and just seconds originally before knowing how to add decimals like me, make sure to change the wait(1) to wait(1 / 10) because the decimals when go down every seconds which im not sure is what you would want

REFERENCE OLD:

for i = (cd * 10),(0 * 10),-1 do
	AbilityDisplay.MainFrame[m].CooldownDisplay.Text = "Cooldown: "..(i/10)
	wait(1)
end

REFERENCE NEW:

for i = (cd * 10),(0 * 10),-1 do
	AbilityDisplay.MainFrame[m].CooldownDisplay.Text = "Cooldown: "..(i/10)
	wait((1 / 10))
end