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

How to prevent people from spamming "E"?

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)

3 comments

0

have you tried making the wait() longer?

0

Use a Server-sided Cooldown Hashmap on the OnServerEvent signal.

0

Struggage wait() is not a problem, because 1 second does take a while.

2 answers

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)

3 comments

0

Doesn't work, I can still spam E on it.

0

ohh i know why

0

so you want to fire RemoteEvent2 and destroy gui after what? if player presses wrong number?

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)

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