Debounce dont work

I have a System, when you click the Textbutton it will send a Remote Event to the Server and you will added into a Table, it also have a Textbutton what also sends a Remote Event to the Server to remove the Player from the Table.
Now i have a problem, if i click the Button fast multiple Times the Script will crash and to avoid it, i maked something like this:

local debounce = false

if debounce == false then
debounce = true

– Code

debounce = false
end

But it dont works. Is it a good Idea to add wait() or do i have another better Options?

local debounce = false

if debounce == false then
debounce = true

–Code
wait(0.1)
debounce = false
end

How long should i wait? wait(1)? wait(0.1)? wait(0.01)

But this dont make any Sense why it dont works without wait, can it be that its taking to long to insert a Player inside the Table?
Could somoene help me, Thanks!

2 Likes
local debounce = false

button.MouseButton1Click:Connect(function()
   if debounce == false then
      --your script
      debounce = true
      wait(1)
      debounce = false
   end
end)

The answer to your question is yes, to add a wait. Because of the crashes, you’ll have to figure out how long you need to wait in order for it not to crash. It just depends on what you’re doing.

1 Like

I just made some edits, sorry.

1 Like

Okay thanks!
And no problem.


1 Like

Glad I could help.

Good luck on your game!

1 Like

I think you had it right the first time.

I tried it and its not working also with the also 1 sec or 5 sec waiting Time. Its crashing everytime.

But thats also not working, the Script working perfectly when i dont press it fast multiple times, it has no waiting Time in the Script.

Change it back to how he had it before he edited it… move the “debounce = true” to above the code.

I have it like that but its not working

Can you show us the current code?

I could send the Game and u can click Start multiple times then it will crash.
Do i need to do the local debounce == false in the Function or outside of the ServerScript?

Then it’s a problem with your code.

The debounce should be in a local script if it’s not. (To prevent the debounce happening to multiple players)

No, this is how a debounce works. It must be a problem with their code.

When the function checks for a false debounce and finds one it should immediately then receive a true debounce before it executes your code, see the API:

This is atleast how i understand debounce… it would defeat the point for it to receive the true debounce after code.

Well, I believe it works both ways. They way I have it, is that it runs the code and then sets the debounce to true and waits 1 second. It’s still waiting the given time and setting to false afterwards.

Does this code work like this? Isn’t it producing a lot of errors?

Edit: i took out some unreferenced debounce lines, but i still get a ton of red and blue errors.
Here is the edited version, the rest of the debounces are good, but I don’t know about the rest of the script and its errors:

local c1 = “9534083234”

local secureStopWaitingTime = 5

game.ReplicatedStorage:WaitForChild(“CreateGame”).OnServerEvent:Connect(function(player, sC, sGM, sJ, pC)


	local secureStop = false

	if secureStop == false then
		secureStop = true

		if not table.find(playersAlreadyInGameMatch, tostring(player.UserId)) then

			if sGM == 1 and sJ == 1 or sJ == 2 and pC == 0 then

				if sC == 1 then

					table.insert(playersAlreadyInGameMatch, tostring(player.UserId))

					gameMatches[tostring(player.UserId)] = {}

					table.insert(gameMatches[tostring(player.UserId)], player.Name)

					table.insert(gameMatches[tostring(player.UserId)], sC)

					table.insert(gameMatches[tostring(player.UserId)], sJ)

					table.insert(gameMatches[tostring(player.UserId)], pC)

					game.ReplicatedStorage:WaitForChild("CreateGame"):FireAllClients(player.Name, sC, sJ, player.UserId)

				end

			elseif sGM == 2 and sJ == 0 and pC == 0 then

				if sC == 1 then

					local reserveServerNumber = teleportService:ReserveServer(chapter1)

					teleportService:TeleportToPrivateServer(c1,reserveServerNumber,{player})

				end

			end

		end

		wait(secureStopWaitingTime)

		secureStop = false

	end


end)

game.ReplicatedStorage:WaitForChild(“JoinGame”).OnServerEvent:Connect(function(player, PWCGMUI)


	local secureStop = false

	if secureStop == false then
		secureStop = true

		if not table.find(playersAlreadyInGameMatch, tostring(player.UserId)) then

			local checkIfPlayerWhoCreatedGameMatchUserIdExists = gameMatches[tostring(PWCGMUI)]

			if checkIfPlayerWhoCreatedGameMatchUserIdExists then

				table.insert(playersAlreadyInGameMatch, tostring(player.UserId))

				table.insert(gameMatches[tostring(PWCGMUI)], tostring(player.UserId))

				game.ReplicatedStorage:WaitForChild("JoinGame"):FireAllClients(player.UserId, PWCGMUI)

			end
		end

		wait(secureStopWaitingTime)

		secureStop = false

	end


end)


local checkIfPlayerWhoCreatedGameMatchUserIdExists = gameMatches[tostring(PlayerWhoCreatedGameMatchUserId)]

if checkIfPlayerWhoCreatedGameMatchUserIdExists then

	table.insert(playersAlreadyInGameMatch, tostring(player.UserId))

	table.insert(gameMatches[tostring(PlayerWhoCreatedGameMatchUserId)], tostring(player.UserId))

	game.ReplicatedStorage:WaitForChild("JoinGame"):FireAllClients(player.UserId, PlayerWhoCreatedGameMatchUserId)

end





game.ReplicatedStorage:WaitForChild(“LeaveGame”).OnServerEvent:Connect(function(player)


	local secureStop = false

	if secureStop == false then
		secureStop = true

		for plWCGMUI, pWCGM in pairs (gameMatches) do

			if table.find(pWCGM, tostring(player.UserId)) then

				pWCGM[table.find(pWCGM, tostring(player.UserId))] = nil

				playersAlreadyInGameMatch[table.find(playersAlreadyInGameMatch, tostring(player.UserId))] = nil

				game.ReplicatedStorage:WaitForChild("LeaveGame"):FireAllClients(player, "PlayerWhoJoinedGameMatch", plWCGMUI)

			end


		end



		local cIpWCGME = gameMatches[tostring(player.UserId)]

		if cIpWCGME then

			gameMatches[tostring(player.UserId)] = nil

			playersAlreadyInGameMatch[table.find(playersAlreadyInGameMatch, tostring(player.UserId))] = nil

			game.ReplicatedStorage:WaitForChild("LeaveGame"):FireAllClients(player, "PlayerWhoCreatedGameMatch")

		end

		wait(secureStopWaitingTime)

		secureStop = false

	end


end)

Its working i just changed some names to like “sC” because its larger in the realscript it can be that i dont changed all of them. But its working good, if i click it fast then not.

local chapter1 = "9534083234"

local secureStopWaitingTime

local secureStop = false

game.ReplicatedStorage:WaitForChild("CreateGame").OnServerEvent:Connect(function(player, selectedChapter, selectedGameModus, selectedJoinSetting, playerCount)
	
	if secureStop == false then
		secureStop = true
	
	if not table.find(playersAlreadyInGameMatch, tostring(player.UserId)) then
		print("5131515")
			if selectedGameModus == 1 and selectedJoinSetting == 1 or selectedJoinSetting == 2 and playerCount == 0 then
			
			if selectedChapter == 1 then
				
				table.insert(playersAlreadyInGameMatch, tostring(player.UserId))
				
				gameMatches[tostring(player.UserId)] = {}
				
				table.insert(gameMatches[tostring(player.UserId)], player.Name)
				
				table.insert(gameMatches[tostring(player.UserId)], selectedChapter)

				table.insert(gameMatches[tostring(player.UserId)], selectedJoinSetting)
				
				table.insert(gameMatches[tostring(player.UserId)], playerCount)
				
				game.ReplicatedStorage:WaitForChild("CreateGame"):FireAllClients(player.Name, selectedChapter, selectedJoinSetting, player.UserId)
				
			end
			
		elseif selectedGameModus == 2 and selectedJoinSetting == 0 and playerCount == 0 then
			
		if selectedChapter == 1 then
			
				local reserveServerNumber = teleportService:ReserveServer(chapter1)
					
			teleportService:TeleportToPrivateServer(chapter1,reserveServerNumber,{player})
			
			end
			
	end
		
		end
		wait(secureStopWaitingTime)
		secureStop = false
		
	end
	
end)



game.ReplicatedStorage:WaitForChild("JoinGame").OnServerEvent:Connect(function(player, PlayerWhoCreatedGameMatchUserId)
	
	if secureStop == false then
		secureStop = true
	
	if not table.find(playersAlreadyInGameMatch, tostring(player.UserId)) then
		
	local checkIfPlayerWhoCreatedGameMatchUserIdExists = gameMatches[tostring(PlayerWhoCreatedGameMatchUserId)]
	
	if checkIfPlayerWhoCreatedGameMatchUserIdExists then
			
			table.insert(playersAlreadyInGameMatch, tostring(player.UserId))
			print("51321135")
			table.insert(gameMatches[tostring(PlayerWhoCreatedGameMatchUserId)], tostring(player.UserId))
			
			game.ReplicatedStorage:WaitForChild("JoinGame"):FireAllClients(player.UserId, PlayerWhoCreatedGameMatchUserId)
			
			end
		end
		wait(secureStopWaitingTime)
		secureStop = false
		
	end
		
end)

game.ReplicatedStorage:WaitForChild("LeaveGame").OnServerEvent:Connect(function(player)
	
	if secureStop == false then
		secureStop = true
	
	for playersWhoCreatedGameMatchesUserId, playersWhoCreatedGameMatches in pairs (gameMatches) do

		if table.find(playersWhoCreatedGameMatches, tostring(player.UserId)) then
			
			playersWhoCreatedGameMatches[table.find(playersWhoCreatedGameMatches, tostring(player.UserId))] = nil
print("2142412")
			playersAlreadyInGameMatch[table.find(playersAlreadyInGameMatch, tostring(player.UserId))] = nil
			
			game.ReplicatedStorage:WaitForChild("LeaveGame"):FireAllClients(player, "PlayerWhoJoinedGameMatch", playersWhoCreatedGameMatchesUserId)
			
		end


		end
		


	local checkIfplayersWhoCreatedGameMatchesExists = gameMatches[tostring(player.UserId)]

	if checkIfplayersWhoCreatedGameMatchesExists then

		gameMatches[tostring(player.UserId)] = nil
print("412512")
		playersAlreadyInGameMatch[table.find(playersAlreadyInGameMatch, tostring(player.UserId))] = nil
		
		game.ReplicatedStorage:WaitForChild("LeaveGame"):FireAllClients(player, "PlayerWhoCreatedGameMatch")

	end
		wait(secureStopWaitingTime)
		secureStop = false
		
		end
	
end)



players.PlayerAdded:Connect(function(player)
	
	player.CharacterAdded:Connect(function(character)

		character.Humanoid.Died:Connect(function()
			
		
			
			for playersWhoCreatedGameMatchesUserId, playersWhoCreatedGameMatches in pairs (gameMatches) do

				if table.find(playersWhoCreatedGameMatches, tostring(player.UserId)) then

					playersWhoCreatedGameMatches[table.find(playersWhoCreatedGameMatches, tostring(player.UserId))] = nil

					playersAlreadyInGameMatch[table.find(playersAlreadyInGameMatch, tostring(player.UserId))] = nil

					game.ReplicatedStorage:WaitForChild("LeaveGame"):FireAllClients(player, "PlayerWhoJoinedGameMatch", playersWhoCreatedGameMatchesUserId)

				end


			end



			local checkIfplayersWhoCreatedGameMatchesExists = gameMatches[tostring(player.UserId)]

			if checkIfplayersWhoCreatedGameMatchesExists then

				gameMatches[tostring(player.UserId)] = nil

				playersAlreadyInGameMatch[table.find(playersAlreadyInGameMatch, tostring(player.UserId))] = nil

				game.ReplicatedStorage:WaitForChild("LeaveGame"):FireAllClients(player, "PlayerWhoCreatedGameMatch")

			end

		
			
player:LoadCharacter()
			
		
		
	end)
end)
	
	player:LoadCharacter()
	
	local checkIfPlayerAppearanceIsLoaded = player:HasAppearanceLoaded()
	
	if checkIfPlayerAppearanceIsLoaded == false then
		
		player.CharacterAppearanceLoaded:Connect(function(character)
			
			local humanoid = character:WaitForChild("Humanoid")
			
		end)
		
	end
	
	game.ReplicatedStorage:WaitForChild("LoadedValue").Value = true
	
	

end)

	game.Players.PlayerRemoving:Connect(function(player)
		
	for playersWhoCreatedGameMatchesUserId, playersWhoCreatedGameMatches in pairs (gameMatches) do

		if table.find(playersWhoCreatedGameMatches, tostring(player.UserId)) then

			playersWhoCreatedGameMatches[table.find(playersWhoCreatedGameMatches, tostring(player.UserId))] = nil

			playersAlreadyInGameMatch[table.find(playersAlreadyInGameMatch, tostring(player.UserId))] = nil

			game.ReplicatedStorage:WaitForChild("LeaveGame"):FireAllClients(player, "PlayerWhoJoinedGameMatch", playersWhoCreatedGameMatchesUserId)

		end


	end



	local checkIfplayersWhoCreatedGameMatchesExists = gameMatches[tostring(player.UserId)]

	if checkIfplayersWhoCreatedGameMatchesExists then

		gameMatches[tostring(player.UserId)] = nil

		playersAlreadyInGameMatch[table.find(playersAlreadyInGameMatch, tostring(player.UserId))] = nil

		game.ReplicatedStorage:WaitForChild("LeaveGame"):FireAllClients(player, "PlayerWhoCreatedGameMatch")

	end



end)




This should work