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

How to prevent people from spamming "E"?

Asked by 3 years ago

How do I prevent people from spamming E?

local userInputService = game:GetService("UserInputService")
local Player = game:GetService("Players").LocalPlayer

script.Parent.Equipped:connect(function()
    active = false
end)

userInputService.InputBegan:Connect(function(input, gameProcessedEvent)
    if gameProcessedEvent then return end
    if input.UserInputType == Enum.UserInputType.Keyboard then
        if input.KeyCode == Enum.KeyCode.E then
            if active then
                active = true
                script.Parent.RemoteEvent:FireServer()

                wait(1)
                    active = false
        else
                script.Parent.RemoteEvent2:FireServer()
                active = true
                Player.PlayerGui:WaitForChild("GunGUI"):Destroy()
                wait(1)
                    active = false
            end
        end
    end
end)

0
have you tried making the wait() longer? Struggage 10 — 3y
0
Use a Server-sided Cooldown Hashmap on the OnServerEvent signal. Ziffixture 6913 — 3y
0
Struggage wait() is not a problem, because 1 second does take a while. TheBuliderMC 84 — 3y

2 answers

Log in to vote
1
Answered by 3 years ago
Edited 3 years ago

try this, hope you understand what i did

local userInputService = game:GetService("UserInputService")
local Player = game:GetService("Players").LocalPlayer
local active = false -- variable here so every function can see it

script.Parent.Equipped:connect(function()
    active = false
end)

userInputService.InputBegan:Connect(function(input, gameProcessedEvent)
    if gameProcessedEvent then return end
    if input.UserInputType == Enum.UserInputType.Keyboard then
        if input.KeyCode == Enum.KeyCode.E then
            if active == false then -- "if active then" means if active is true, now its checking if active is false
                active = true
                script.Parent.RemoteEvent:FireServer()

                wait(1)
                    active = false
        else
                script.Parent.RemoteEvent2:FireServer()
                active = true
                Player.PlayerGui:WaitForChild("GunGUI"):Destroy()
                wait(1)
                    active = false
            end
        end
    end
end)
0
Doesn't work, I can still spam E on it. TheBuliderMC 84 — 3y
0
ohh i know why jerryisgod29 176 — 3y
0
so you want to fire RemoteEvent2 and destroy gui after what? if player presses wrong number? jerryisgod29 176 — 3y
Ad
Log in to vote
0
Answered by 3 years ago

Im not entirely sure what you are trying to do, but I think it may have to do with your debounce (active).

You are saying if the function is active then make it active, so I assume your active is meant to be false.

You also have a wait, for when active becomes false which I can understand but you might want to put that somewhere else.

You also have the active set back to true everytime you do it. I believe this may work a lot better.

local userInputService = game:GetService("UserInputService")
local Player = game:GetService("Players").LocalPlayer

script.Parent.Equipped:connect(function()
    active = false
end)

userInputService.InputBegan:Connect(function(input, gameProcessedEvent)
    if gameProcessedEvent then return end
    if input.UserInputType == Enum.UserInputType.Keyboard then
        if input.KeyCode == Enum.KeyCode.E then
            if not active then --If the function is active, then it will do the code below.
                active = true --sets it to active.
                script.Parent.RemoteEvent:FireServer()
            else --Otherwise it will run the code below if the function is active.
        --Please note, the function below will still be spammable, you should probably put       
                another check in so people cant spam it. 
                script.Parent.RemoteEvent2:FireServer()
                Player.PlayerGui:WaitForChild("GunGUI"):Destroy()
            end
        wait(1) --This will be how long it will wait before active is reset.
        active = false
        end
    end
end)

Answer this question