Cooldown code not working

so im making a dash script and the cooldown kicks in after using the dash but u can still spam it one time why is this and how do i fix it

local uis = game:GetService("UserInputService")
local PushForce = 100
local plr = game.Players.LocalPlayer
local character = plr.Character
local tweenservice = game:GetService("TweenService")
local cd = true
local cooldown = 100



uis.InputBegan:Connect(function(key)
	if cd == true then 
		if key.KeyCode == Enum.KeyCode.Q then
		print(tostring(cd)..' now')
		cd = false
		print(tostring(cd)..' after')
		
		character.Archivable = true
		local charclone = character:Clone()
		charclone:WaitForChild("Humanoid").DisplayDistanceType = Enum.HumanoidDisplayDistanceType.None
		charclone.Parent = plr.PlayerGui.ScreenGui.ViewportFrame
		for i,v in pairs(charclone:GetDescendants()) do
			if v:IsA("BasePart") then
				v.Anchored = true
				local goal = {}
				goal.Transparency = 1
				local tween = tweenservice:Create(v,TweenInfo.new(1,Enum.EasingStyle.Sine,Enum.EasingDirection.In,0),goal)
				tween:Play()
				tween.Completed:Connect(function()
					tween:Remove()
					v:Remove()
				end)
			end
		end
		game:GetService("Debris"):AddItem(charclone,1)
		local bodyvelocity = Instance.new("BodyVelocity")
		bodyvelocity.MaxForce = Vector3.new(1e9,1e9,1e9)
		bodyvelocity.Velocity = game.Players.LocalPlayer.Character:WaitForChild("Humanoid").MoveDirection * PushForce
		bodyvelocity.Parent = game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart")
		game:GetService("Debris"):AddItem(bodyvelocity,0.1)
		
		
		
		wait(cooldown)
			cd = true
			print("set back to "..tostring(cd))
		end
	end
end)
2 Likes

simple CD:

local uis = game:GetService(“UserInputService”)

local debounce = false

uis.InputBegan:Connect(function(input, isTyping)
if isTyping then return end – checks if player is typing, if returned true then end code

if input.KeyCode == Enum.KeyCode.Q then
	if debounce == false then
		debounce = true
		
		
		
		print("aa")
		
		
		task.wait(1)
		debounce = false
	end
end

end)

1 Like

ill try this but im pretty sure i will get the same outcome

and it dindt work i found the issue tho it is because of the tween wich yields it and therefore u have to wait for the animation to finish before it like works i removed the animation and it works for now that’ll do but i still want the animation