Tick Cooldown Not Working

Hello developers! I have been working on an energy system recently and
I wanted to add a small cooldown so a player doesn’t take 3-10 steps per move.
The script was working perfectly so far with a wait (0.1) however I did some
research and figured out that’s a bad practice and moved to tick (). The tick cooldown
I have added is not adding the cooldown that wait did and was wondering how I could fix it.

local player = game.Players.LocalPlayer
local Char = player.CharacterAdded:Wait ()
local Humanoid = Char:WaitForChild ("Humanoid")
local runService = game:GetService("RunService")
local Stepped = false
local start = tick()

runService.Heartbeat:Connect (function()
	
if Humanoid.MoveDirection.Magnitude > 0 then
		if not Stepped then
			Stepped = true
		    warn("Step Taken")
			--Add Action here
			if tick () - start >= .1 then
				Stepped = false
			end
		end	
	end		
end)
1 Like

There’s a space between tick and (), that may be why. Try fixing that and see how it goes.

Also, just a little suggestion, I recommend you use os.time() instead of tick().

2 Likes

tick() is deprecated. use os.time()

1 Like

also there is a space there lol

Cause be because you’re only detecting for a Character Model to only be added? Couldn’t you just reference the Player’s Character as well?

@httpDerpyy @foundanerror I don’t think the space would matter anyways, there have been instances where spaces between functions do work

local player = game.Players.LocalPlayer
local Char = player.Character or player.CharacterAdded:Wait()
local Humanoid = Char:WaitForChild("Humanoid")
local runService = game:GetService("RunService")
local Stepped = false
local start = os.time()

runService.Heartbeat:Connect(function()
    if Humanoid.MoveDirection.Magnitude > 0 then
		if not Stepped then
			Stepped = true
		    warn("Step Taken")
			--Add Action here
			if os.time() - start >= .1 then
				Stepped = false
			end
		end	
	end		
end)
2 Likes

the space doesnt matter like you said but it is better practice to do wait() other then having spaces cause your code could get messy

1 Like

Thanks for the reply. I have tested the code but the cool-down isn’t effective and each time I take a step it adds +3 or more to the output.

This is probably due to Heartbeat running so often, since the MoveDirection property is still resulting as a number that’s greater than zero every time it’s called for & detecting the change (Or firing every 1/40ths of a second)

Although I am unsure if the MoveDirection can go a value greater than one :thinking:

@foundanerror Fair enough, I never thought of it that way

1 Like

tick isn’t deprecated yet, but will soon be. You want something equivalent to tick as OP is using it, os.clock should be used instead as it has the highest accuracy (more than tick) and won’t be deprecated soon, so your advice on using os.time isn’t good.

PS:

For time sensitive things, you want the outmost accuracy as well, os.time returns whole numbers and isn’t as accurate as tick or os.clock.

1 Like

You haven’t set a new tick() to start, hence your code the difference between tick() and start will always be greater than 0.1 seconds. I’d suggest setting a new tick() to start after setting Stepped to false.