# Does someone see a misstake in the function?

Sup guys,
having a problem with a function, it works but it wont end.
The bar size increases every loop and should end at the (2) seconds

local ReplicatedStorage = game:GetService(“ReplicatedStorage”)
local MiningClientEvent = ReplicatedStorage:WaitForChild(“MiningClient”)
local seconds = 2
local waitseconds = seconds / 10
local secondswaited = 0
local count = 0;
local mining = false

local function MiningEvent()
local player = game.Players.LocalPlayer
local Bar = player.PlayerGui.PlayerHUD.MiningBar.Frame.top
Bar.Size = UDim2.new(0, 0, 1, 0)
mining = true

``````while(mining == true) do
secondswaited = secondswaited + waitseconds
count = count + 0.1
Bar.Size = UDim2.new(count, 0, 1, 0)
print("waited: "..secondswaited.." seconds: "..seconds)
if(secondswaited == seconds) then
print("mining false")
mining = false
end
wait(waitseconds)
end
``````

end
MiningClientEvent.OnClientEvent:Connect(MiningEvent)

Print says:
waited: 1.8 seconds: 2 (this script)
waited: 2 seconds: 2 (this script, the function should end here?)
mining (server)
waited: 2.2 seconds: 2 (this script)
waited: 2.4 seconds: 2 (this script)

I dont know whats wrong with that, 2 should be 2?

Change

``````if(secondswaited == seconds) then
``````

to

``````if(secondswaited >= seconds) then
``````

this is a problem with floating point precision.

If you print

``````print(math.ceil(secondswaited))
``````

after the loop, you will notice that it will print 3 instead of 2, but math.ceil(2) should return 2 so `secondswaited` is something more than 2.

That’s because the binary fraction expansion of 1/10 is infinite so in order to store that number in a finite computer memory it has to be rounded at some point which causes that error in the representation of that number.

1 Like