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)
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)
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
@foundanerror Fair enough, I never thought of it that way
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.
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.