Hello guys, i’m a beginner scripter and i was making a time trial game,
I don’t know why i keep getting these weird numbers:
I have no idea how to fix it, i dont know many things about scripting, so any of you can help me to fix it.
I tried many things, and it still didnt work.
local timer = script.Parent.Timer
local startPart = workspace.start
local endPart = workspace.endPart
startPart.Touched:Connect(function()
timer.TextColor3 = Color3.fromRGB(133, 255, 112)
for i = 0, (1/0), 0.01 do
repeat
timer.Text = i
task.wait()
until endPart.Touched
end
end)
endPart.Touched:Connect(function()
if tonumber(timer.Text) > 0 then
timer.TextColor3 = Color3.fromRGB(255, 255, 255)
end
end)
What you’re experiencing is know as a ‘floating point error’ and it’s caused by how decimal numbers are represented by computers. I won’t go into the specifics of why this happens because it’s beyond the scope of what you need to know at the moment but if you study computer science at school, you will likely cover it at some point.
The easiest and most common way to deal with this issue is simply to round or truncate the result.
I’m guessing you want to retain two decimal places of accuracy and so the correct method for rounding would be.
function roundToHundredths(numberToRound)
numberToRound *= 100
local result = math.round(numberToRound)
result /= 100
return result
end
If you want a more versatile version of this function, you can check out this on the lua-users wiki.
If you want a more indepth explanation regarding why this issue occurs, then you can take a look at this webpage.
Note that neither of these webpages are Roblox sanctioned sites, so proceed with caution.