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

How do I make my debounce for remote event for each player not just for everyone?

So I succesfully made my debounce in a remote event and it goes like this

When A player clicks activate I fire a remote and

local Debounce = false


game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(player)
	local Tool = game.Workspace[player.Name]:FindFirstChildWhichIsA("Tool")
	local Configuration = Tool.Configuration
	
	
	if Tool and Debounce == false then
			print('g')

		Debounce = true 
		
		wait(Configuration.FireRate.Value)

		Debounce = false		
	end


end)

But I . noticed that this works for everyone not each player meaning

if one person fired it the other person has to wait

2 comments

0

Do the debounce in the LocalScript.

1

But then the exploits bro

1 answer

I would use a dictionary.

playerDebounces = {}

In this dictionary, you'll store a key for every player, and a boolean value, assigned to that key, defaulted to false, indicating whether or not they can fire the RemoteEvent.

We need to put every player in the dictionary.

game.Players.PlayerAdded:Connect(function(Player)
	if playerDebounces[Player.Name] == nil then
		playerDebounces[Player.Name] = false
	else
end)

game.Players.PlayerRemoving:Connect(function(Player)
	playerDebounces[Player.Name] = nil
end)

Whenever this RemoteEvent is fired, modify the value of the key that pertains to the player that fired the RemoteEvent.

The bit of code above doesn't account for players that are already in the game, it only accounts for players that have yet to either enter or leave the game. So, we have to make sure that the playerDebounces dictionary has a key for the Player that fired the RemoteEvent, and if it doesn't, create one and set it to false(because this certainly is the first time the Player is firing the RemoteEvent).

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(Player)
	local existent = false
	for i = 1, #playerDebounces do
		if playerDebounces[i] == Player.Name then
			existent = true
			break
		end
	end
	if not existent then
		playerDebounces[Player.Name] = false
	end
	if playerDebounces[Player.Name] == false then
		playerDebounces[Player.Name] = true
		wait(5)
		playerDebounces[Player.Name] = false
	end
end)

And this is the completed portion.

playerDebounces = {}

game.Players.PlayerAdded:Connect(function(Player)
	if playerDebounces[Player.Name] == nil then
		playerDebounces[Player.Name] = false
	else
end)

game.Players.PlayerRemoving:Connect(function(Player)
	playerDebounces[Player.Name] = nil
end)

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(Player)
	local existent = false
	for i = 1, #playerDebounces do
		if playerDebounces[i] == Player.Name then
			existent = true
			break
		end
	end
	if not existent then
		playerDebounces[Player.Name] = false
	end
	if playerDebounces[Player.Name] == false then
		playerDebounces[Player.Name] = true
		wait(5)
		playerDebounces[Player.Name] = false
	end
end)

I have a 6th sense telling me there's a much simpler and quicker solution than mine, assuming that mine even works to begin with. If anyone has a better solution, or even a working one, post it.

1 comment

1

You have the right idea, but you don't need the PlayerAdded part -- you can just switch between "nil" and "true" instead of "false" and "nil" - this means you won't need lines 14-23. The "if" on line 24 should then just say "if not playerDebounces[Player] then" - you can just use "Player" as the key instead of "Player.Name" everywhere

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