How to fix for i loop number being different

I’m having this issue where while the cooldown is counting down the number starts going beyond and showing numbers like 0.1000004 or something like that.
Here’s a gif to show what happened.
https://i.gyazo.com/10c80f2a06677e7239eb8ac2e5794c65.gif

I thought math.round would work but unfortunately it just shows 0 instead in the cooldown.

Here’s the code that is related to the issue.

script.Parent.Activated:Connect(function()
	if Ready == true then
		Ready = false
		Attack1()
		AttackEvent:FireServer(Player)
		
		for i = Cooldown.Value,0,-0.1 do
			Tool.Name = i
			task.wait(0.1)
		end
		Tool.Name = "Flame Punch"
		Ready = true
	end
end)

that is just a problem with the computers math thing not your code itself.

But you could just make it a string and split it from the “.” and get rid of the rest

string.split(tostring(123.123123), ".")[1]

which prints

123

So here is what it would look like

script.Parent.Activated:Connect(function()
	if Ready == true then
		Ready = false
		Attack1()
		AttackEvent:FireServer(Player)
		
		for i = Cooldown.Value,0,-0.1 do
			Tool.Name = string.split(tostring(i), ".")[1] -- Will remove all numbers after the "."
			task.wait(0.1)
		end
		Tool.Name = "Flame Punch"
		Ready = true
	end
end)

I actually want to keep only 1 number after the “.” like the cooldown should be counting down like this:
0.4, 0.3, 0.2, 0.1, 0

In this case, I would take the number, multiply it by 10 to shift the decimal over to the right by one place, floor the number, then divide it by 10 to move the decimal back. This has never created a ridiculously long decimal in my experience.

local num = 5.28463
local new = math.floor(num * 10) / 10
print(new) --// Should print 5.2
2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.