Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to make it so that the numbers don't round off?

This is the timer script for my upcoming Ninja Warrior tournament and I'm having trouble trying to count by tenths of a second. When I do try it, the first 10 intervals (90 to 89) display normally, and then each other number is off by about .0000000004, which is called roundoff error. How do I fix this so it doesn't give me crazy numbers?

script.Parent.MouseClick:Connect(function()
	for i = 90,0,-0.1 do
		for _,p in pairs(game.Players:GetPlayers()) do
			p.PlayerGui.ScreenGui.Timer.Text = tostring(i)
		end
		if i == 0 then
			for _,u in pairs(game.Players:GetPlayers()) do -- "u" is a metaphor variable that stands for "User"
				u.PlayerGui.ScreenGui.Timer.Text = "Failure!"
			end
			game.Workspace:FindFirstChild("Clear", true).Disabled = true
		end
	end
end)

1 answer

✓ Accepted answer

There are different ways of doing this, but I'll explain the most simple and straightforward way.

Count down from 900 by 1, then divide by 10.

script.Parent.MouseClick:Connect(function()
    for i = 900,0,-1 do
        for _,p in pairs(game.Players:GetPlayers()) do
            p.PlayerGui.ScreenGui.Timer.Text = i/10
        end
        if i == 0 then
            for _,u in pairs(game.Players:GetPlayers()) do -- "u" is a metaphor variable that stands for "User"
                u.PlayerGui.ScreenGui.Timer.Text = "Failure!"
            end
            game.Workspace:FindFirstChild("Clear", true).Disabled = true
        end
    end
end)

Now I'll do you another favour and save you some future trouble. The Server cannot access PlayerGui. So it's best you do the counting client-side.

-- Local script inside the TextLabel
ClickDetector.MouseClick:Connect(function()
	for i = 900,0,-1 do
		script.Parent.Text = i/10
		wait(1)
	end
	script.Parent.Text = "Failiure!"
end)

Be sure to Upvote/Accept if I helped!

This discussion is preserved as it was. New answers, comments, and votes are closed.