Cooldown on dash

this script is a dash script, double tap W and you will dash. nothing is wrong with it except the local cd is supossed to act as a 5 seconds cooldown. the bad thing is it doesnt. why doesnt it work?

local plr = game.Players.LocalPlayer
local Char = plr.Character or plr.CharacterAdded:Wait()
local UserInputService = game:GetService("UserInputService")
local Tapped = false
local cd = false

if cd == false then
	cd = true
	UserInputService.InputBegan:Connect(function(Input, GameStuff)
		if GameStuff then return end
		if Input.KeyCode == Enum.KeyCode.W then
			if not Tapped then
				Tapped = true
				wait(0.3)
				Tapped = false
			else
				Char.HumanoidRootPart.Velocity = Char.HumanoidRootPart.CFrame.lookVector*330
				local Animation = plr.Character:FindFirstChild("Humanoid"):LoadAnimation(script.Animation)
				Animation:Play()
				wait(5)
				cd = false
			end
		end
	end)
end

some of the " marks will be signed wrong.

You do not seem to be altering the Var Gamestuff, which when set to false prevents the player from sprinting I think.
I could be wrong, I am trying to see if there is anything else in the script I missed

1 Like

its not sprinting, its a dash. theres nothing wrong with the dash its just i need a cooldown because otherwise players can essentially fly

Possibly cause cd is never sent to false agian. But only in an if else that runs for a very short time.

I think its because that script only functioned 1 time, so I think when you press the key and you check the cd value will be much better

-- maybe this?
local plr = game.Players.LocalPlayer
local Char = plr.Character or plr.CharacterAdded:Wait()
local UserInputService = game:GetService("UserInputService")
local Tapped = false
local cd = false

	UserInputService.InputBegan:Connect(function(Input, GameStuff)
              if cd == false then
                cd = true
		if GameStuff then return end
		if Input.KeyCode == Enum.KeyCode.W then
			if not Tapped then
				Tapped = true
				wait(0.3)
				Tapped = false
			else
				Char.HumanoidRootPart.Velocity = Char.HumanoidRootPart.CFrame.lookVector*330
				local Animation = plr.Character:FindFirstChild("Humanoid"):LoadAnimation(script.Animation)
				Animation:Play()
				wait(5)
				cd = false
			end
		end
         end
end)

nope, i cant dash anymore with this

whats the error on the output??

nothing pops up in the output.

Cd has to be false to run the whole dash script, and its set to false the minute you press W or any key.

1 Like

so any ideas? on what to change?

Hmm maybe delete the line which sets cd to true right after it checks for cd=false
And maybe replace the code in the else with this

Char.HumanoidRootPart.Velocity = Char.HumanoidRootPart.CFrame.lookVector*330 
cd=true
				local Animation = plr.Character:FindFirstChild("Humanoid"):LoadAnimation(script.Animation)
				Animation:Play()

				wait(5)
				cd = false

It sets Cd to true right after the script is run, and it sets cd to false agian after the 5 second cooldown.

it doesnt seem to be working either…

Here it is here, It should work, I replaced all the code for you

1 Like

Seems like I’m a bit late, but a better way of keeping track of when the player pressed ‘W’ would be using os.clock()

local UserInputService = game:GetService("UserInputService")

local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()

local debounce = false
local lastTime = os.clock()

UserInputService.InputBegan:Connect(function(input, gameProcessed) 
    if gameProcessed or debounce then 
        return
    end

    if os.clock() - lastTime < 0.3 then — time between taps
        debounce = true
        — dash code


        wait(2) — cooldown
        debounce = false
    end

    lastTime = os.clock() — reset timer
end)
1 Like