How to add a delay on my dash script?

hi so I’m trying to add a delay to my dash script so players don’t fly
i tried adding wait() on it but it seem it doesn’t work

local plr = game.Players.LocalPlayer
local Char = plr.Character or plr.CharacterAddeed:Wait(5)
local UserInputSerivce = game:GetService("UserInputService")

local humanoid = script.Parent:WaitForChild("Humanoid")
local anim = humanoid:LoadAnimation(script:WaitForChild("Animation"))

UserInputSerivce.InputBegan:Connect(function(Input, GameStuff)
	if GameStuff then return end
	if Input.KeyCode == Enum.KeyCode.LeftShift then
		Char.HumanoidRootPart.Velocity = Char.HumanoidRootPart.CFrame.lookVector*150 
		anim:Play()
		print("Dash!")
	end
end)

here’s the Localscript i put it in StarterCharacterScripts

this might help you out

1 Like

Use a variable as a debounce like so:

local plr = game.Players.LocalPlayer
local Char = plr.Character or plr.CharacterAddeed:Wait(5)
local UserInputSerivce = game:GetService("UserInputService")

local onCooldown = false
local humanoid = script.Parent:WaitForChild("Humanoid")
local anim = humanoid:LoadAnimation(script:WaitForChild("Animation"))

UserInputSerivce.InputBegan:Connect(function(Input, GameStuff)
	if GameStuff then return end
	if Input.KeyCode == Enum.KeyCode.LeftShift then
		if not onCooldown then
			onCooldown = true
			Char.HumanoidRootPart.Velocity = Char.HumanoidRootPart.CFrame.lookVector*150 
			anim:Play()
			print("Dash!")
			wait(5) --Wait 5 seconds cooldown
			onCooldown = false
		end
	end
end)

1 Like

Thank you so much this helps a lot :+1:

1 Like