Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How would I prevent the spam of this UserInput?

Asked by 7 years ago
Edited 7 years ago

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

Log in to vote
0
Answered by 7 years ago
Edited 7 years ago

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)
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. SevenRubyRobes 6 — 7y
Ad

Answer this question