How could i make a debounce for each player inside a script?

Hello there!
Inside a script of mine i have a problem that the debounce i use breaks after a second player that runs the same script.
I want to have a seperate debounce for each player that tries to run that exact script.
I know that i have to use tables but i am not really good at them.
Is there a sollution to my problem?

Any suggestion whould be greatly appreciated!

Something along the lines of this, but not sure your use case but this should put you on the right track!

local Debounces = {}

Debounces[Player] = true;

if Debounces[Player] then 
    task.wait();
    Debounces[Player] = false;
end

Debounces[Player] = false;
1 Like

Solution of an existing post:

local playerTable = {} -- create an empty table to store an index for everyone in
local someRemoteEvent = game.ReplicatedStorage.RemoteEvent-- an example event
local cooldownTime = 4 -- used 4 seconds as an example

game.Players.PlayerAdded:Connect(function(player)
	if not playerTable[player.Name] then -- if there isn't an entry for the player already
		playerTable[player.Name] = tick() -- Create a new entry in the table for each new player, and make the index their name so we can access it later
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	if playerTable[player.Name] then -- if there's an entry of the player in the table
		playerTable[player.Name] = nil -- remove it from the table
	end
end)

someRemoteEvent.OnServerEvent:Connect(function(player)
if tick() - playerTable[player.Name] > cooldownTime then -- If the time is greater than the cooldown time (so if more than 4 seconds have passed since the last time you set the player's index in the playerTable to tick()
		playerTable[player.Name] = tick() -- set it back to tick() so it resets
   --CODE HERE
end
end)

Get more info on how this works and the whole thread:

3 Likes
local Debounce

--code here blah blah
game.Players.PlayerAdded:Connect(function(plr)
	if plr then
      Debounce == true
		if Debounce == true then
			print("debounce = true")
		elseif Debounce == false then
			print("debounce = false")
		end
	end
end)

Please refer to this post:

1 Like

That wont work for what he is asking

1 Like

+1 for Interactivated’s solution. I’ve used it numerous times and it works well

1 Like