Trying to set a cooldown for this attack up

local UserInputService = game:GetService("UserInputService")
local cd = 10
local pressdeb = false


UserInputService.InputBegan:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.Q then
        if pressdeb == false then
            pressdeb = true
            print("PressDown")
        end
    end
end)

UserInputService.InputEnded :Connect(function(input)
    if input.KeyCode == Enum.KeyCode.Q then
	if pressdeb == true then
			pressdeb = false
            print("PressUp")
            wait(cd)
        end
    end
end)

How do I get this so that it has a cooldown, cause that “wait(cd)” does nothing and the attack can print “PressUp” and “PressDown” over and over again

2 Likes

Hello, how are you doing?
Sorry about before, i tried giving an answer but i immediately went across something.

local UserInputService = game:GetService("UserInputService")
local cd = 0
local pressdeb = false

UserInputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.Q then
		if pressdeb == false then
			pressdeb = true
			print("PressDown")
		end
	end
end)

UserInputService.InputEnded:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.Q then
		if pressdeb == true then
			pressdeb = false
			print("PressUp")
			if cd <= 0 then
				cd = 10
				--you're code
			end
			while cd >= 0 do
				wait(1)
				cd = cd - 1
			end
		end
	end
end)

Try this code out, it should work!

I think that this code might work:

local UserInputService = game:GetService("UserInputService")
local coolingdown = false
local cd = 10
local pressdeb = false

UserInputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.Q then
		if coolingdown == false then
			if pressdeb == false then
				pressdeb = true
				print("PressDown")
			end
		end
	end
end)

UserInputService.InputEnded:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.Q then
		if pressdeb == true then
			pressdeb = false
			print("PressUp")
			coolingdown = true
			wait(cd)
			coolingdown = false
		end
	end
end)

you may have to test this, tell me if its wrong and ill try and help!

Reason why it keeps happening, is that you keep setting the pressdeb variables to true, then false after changing your User Input which won’t really change any difference

Try this:

local UserInputService = game:GetService("UserInputService")
local cd = 0
--local pressdeb = false


UserInputService.InputBegan:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.Q then
        if pressdeb == false then
            pressdeb = true
            print("PressDown")
        end
    end
end)

UserInputService.InputEnded :Connect(function(input)
    if input.KeyCode == Enum.KeyCode.Q then
	if pressdeb == true and cd == 0 then
	        cd = 10
            print("PressUp")
            while cd >= 0 do 
                wait(1)
                cd -= 1
            end
            pressdeb = false
        end
    end
end)