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

How do I fix this cooldown problem that when you press K it doesn't get the debounce?

Ok, so I'm trying to add a cooldown to my gui that when you press k the gui opens with a smooth animation and you have to wait until the cool down that is 6 seconds long but it, went wrong, and then, something went wrong in the output and you still can spam the gui to open.

The code:

local Plr = game.Players.LocalPlayer
local mainframe = script.Parent


local debounce = false
Plr:GetMouse().KeyDown:Connect(function(K)
	if not debounce then
		debounce = true
	end
	if K == "k" then
		local object = script.Parent
		object.Position = UDim2.new(0.183, 0,0.999, 0)

		wait(0.1)

		object:TweenPosition(UDim2.new(0.183, 0,0.875, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Quint)
		
		wait(6)
		debounce = false
	end
end)

1 answer

✓ Accepted answer

Try doing the debounce like this. Also I recommend you use UserInputService rather than Player:GetMouse().

local Plr = game.Players.LocalPlayer
local mainframe = script.Parent


local debounce = false
Plr:GetMouse().KeyDown:Connect(function(K)
    if not debounce then
        debounce = true

    if K == "k" then
        local object = script.Parent
        object.Position = UDim2.new(0.183, 0,0.999, 0)

        wait(0.1)

        object:TweenPosition(UDim2.new(0.183, 0,0.875, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Quint)

        wait(6)
        debounce = false
    end
end)

3 comments

0

Ok, I'm gonna try it.

0

Thank you so much!

0

No problem.

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