Cooldowns off by half a second

So I have a cooldown system entirely on the client (yes ik its not exploit safe) and for some reason it is off by half a second.

Cooldown modulescript:

local CDSystem = {}

function roundNumber(num, numDecimalPlaces)
	return tonumber(string.format("%." .. (numDecimalPlaces or 0) .. "f", num))
end



function CDSystem.Init(Player:Player)
	
	local ClientCD = {}
	
	function ClientCD.AddCooldown(CooldownName:string, CooldownTime:number)
		local Cooldown = script.CooldownBase:Clone()
		Cooldown.Parent = Player.PlayerGui.MenuGUI.CooldownBox
		coroutine.resume(coroutine.create(function()
			Cooldown.CDName.Text = CooldownName
			for i=.1, CooldownTime, .1 do
				Cooldown.CDTime.Text = roundNumber(tostring(CooldownTime), 1)
				CooldownTime -= .1
				task.wait(.1)
			end
			Cooldown:Destroy()
		end))
	end
	
	return ClientCD
end



return CDSystem

Local cd test:

local CDSystem = require(game.ReplicatedStorage.ClientModules.CooldownSystem).Init(game.Players.LocalPlayer)
for i=1, 30 do
	CDSystem.AddCooldown("Test Cooldown", 5)
	task.wait(5)
end

A new cooldown is started when the current cd is still at .5 seconds left.
When i change the line to

CDSystem.AddCooldown("Test Cooldown", 4.5)

then it is correctly on time… why is this? and how do i fix it?

I think your task.wait(0.1) function is inaccurate and it may wait 0.11 seconds instead of 0.1 seconds. These inaccuracies will add up and make the function last longer since you will be waiting longer.
task.wait(.1)

I suggest changing task.wait(.1) to task.wait(0.095) to account for the delay.
task.wait(.095)

2 Likes

I will try it out, really hope it works because the cooldowns can be very off…