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

How would I prevent the spam of this UserInput?

I'm currently working on a menu Gui, and I'm trying to make it to where upon pressing T, the Gui will drop down into view, and upon pressing it again, the Gui will slide back up.

So far, it works perfectly, however the only problem is, I'm unsure how to prevent players from spamming the T key and making the Gui not work correctly.

(Also, all of the waits I'm using was my attempt to provide a cooldown.) Here's my code:

local userInput = game:GetService("UserInputService")
local background = game.Lighting.MenuBackground

userInput.InputBegan:Connect(function(input, gameProcessed)
	wait(0.1)
	if input.KeyCode == Enum.KeyCode.T and background.Enabled == false then
		wait(0.1)
		script.Parent.FrameXY:TweenPosition(UDim2.new(0.1, 0.1, 0.3, 0.1), "Out", "Bounce", 0.6)
		wait(0.1)
		background.Enabled = true
	elseif input.KeyCode == Enum.KeyCode.T and background.Enabled == true then
		wait(0.1)
		script.Parent.FrameXY:TweenPosition(UDim2.new(0.1, 0, -0.4, 0), "Out", "Quad", 0.6)
		wait(0.1)
		background.Enabled = false
	end
end)

Any help?

1 answer

✓ Accepted answer

You could use a boolean variable - set it to true when the gui completes its animation and check for it when the user presses T.

EDIT: Something like this, maybe:

local userInput = game:GetService("UserInputService")
local dropped = false
local finishedAnim = true

userInput.InputBegan:Connect(function(input, gameProcessed)
    if input.KeyCode == Enum.KeyCode.T and finishedAnim and not dropped then
		finishedAnim = false
        -- drop-down animation
		dropped = true
		finishedAnim = true
    elseif input.KeyCode == Enum.KeyCode.T and finishedAnim and dropped then
        finishedAnim = false
		-- going up
		dropped = false
		finishedAnim = true
    end
end)

1 comment

0

This is something I actually tried before I wrote the current script. I couldn't get it to work, but I'll try again.

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