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

How to make a gui button that can't be spammed?

I have a gui button that when you click on it it respawns the player. The problem is, if you spam the button it crashes the game. I've tried to make a bool value to solve this but you can still spam it. Any solutions?

local player = game.Players.LocalPlayer
local cooldown = script.Parent.Value
cooldown = false

if cooldown == false then
function OnClicked() 

player:LoadCharacter()

cooldown = true
wait(5)
end 
end
cooldown = false
script.Parent.MouseButton1Down:connect(OnClicked)print("player respawned")

1 comment

0

Is your code inside of a big loop?

1 answer

First off, cooldown should not be a value class, but a value variable and when you check cooldown for false you should set it to true right afterwards.

Secondly, your cooldown = false line needs to be inside of the if cooldown == false statement. When combined it all should look like this:

local player = game.Players.LocalPlayer
local cooldown = script.Parent.Value
cooldown = false

if cooldown == false then
	cooldown = true
	function OnClicked() 
	player:LoadCharacter()
	wait(5)
	cooldown = false
	end 
end
script.Parent.MouseButton1Down:connect(OnClicked)print("player respawned")

7 comments

0

did you block the code? because after code 5 you didn't code block it

0

Fixed it

0

hmm... strange. you can still spam it?

0

Is this inside of a loop?

0

im pretty sure it isnt

0

Nevermind, I realised it could be still be spammed since the gui script would reset when ever the player reloaded. So, I made a simple solution (which makes me look stupid because how simple it is). I added a "wait(5)" in the beginning of the script so that you have to wait 5 seconds before you can press the button

0

It's called a debounce.

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