Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to add a cool down on my "Dash Script"?

I am making a dash script and successfully did it but the players spams it. i want to fix this but i don't know how to do it.

The Code:

local player = game.Players.LocalPlayer
repeat wait() until player.Character.Humanoid````
local humanoid = player.Character.Humanoid
local mouse = player:GetMouse()

local anim = Instance.new("Animation")
anim.AnimationId = "http://www.roblox.com/asset/?id=1824890937" 

mouse.KeyDown:connect(function(key)
	if string.byte(key) == 50 then
		local playAnim = humanoid:LoadAnimation(anim)
		playAnim:Play()
		script.Sound:Play()
	player.Character.HumanoidRootPart.Velocity = player.Character.HumanoidRootPart.CFrame.lookVector * 125
        wait(1.5)
	end
	wait(10)
end)

1 answer

✓ Accepted answer

Assign the current machine time tick() to an upvalue (variable in an outer scope), subtract tick() from that variable and check if it's larger than an arbitrary number.

Example

local lastClicked, cooldown = 0, 2


UserInputService.InputBegan:Connect(function(input, gameProcessed)
	if tick() - lastClicked >= cooldown then
		lastClicked = tick()
		-- ...
	end
end)

2 comments

0

Where do i put it tho? im a newbie at scripting sorry

0

on your if statement in line 50 you can make the condition 'string.byte(key) == 50 and tick() - lastClicked > cooldown' or something similar.

This discussion is preserved as it was. New answers, comments, and votes are closed.