Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by 5 years ago

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

01local Debounce = false
02 
03 
04game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(player)
05    local Tool = game.Workspace[player.Name]:FindFirstChildWhichIsA("Tool")
06    local Configuration = Tool.Configuration
07 
08 
09    if Tool and Debounce == false then
10            print('g')
11 
12        Debounce = true
13 
14        wait(Configuration.FireRate.Value)
15 
16        Debounce = false       
17    end
18 
19 
20end)

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

if one person fired it the other person has to wait

0
Do the debounce in the LocalScript. Unhumanly 152 — 5y
1
But then the exploits bro Overseer_Prince 188 — 5y

1 answer

Log in to vote
2
Answered by
Unhumanly 152
5 years ago
Edited 5 years ago

I would use a dictionary.

1playerDebounces = {}

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.

1game.Players.PlayerAdded:Connect(function(Player)
2    if playerDebounces[Player.Name] == nil then
3        playerDebounces[Player.Name] = false
4    else
5end)
6 
7game.Players.PlayerRemoving:Connect(function(Player)
8    playerDebounces[Player.Name] = nil
9end)

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).

01game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(Player)
02    local existent = false
03    for i = 1, #playerDebounces do
04        if playerDebounces[i] == Player.Name then
05            existent = true
06            break
07        end
08    end
09    if not existent then
10        playerDebounces[Player.Name] = false
11    end
12    if playerDebounces[Player.Name] == false then
13        playerDebounces[Player.Name] = true
14        wait(5)
15        playerDebounces[Player.Name] = false
16    end
17end)

And this is the completed portion.

01playerDebounces = {}
02 
03game.Players.PlayerAdded:Connect(function(Player)
04    if playerDebounces[Player.Name] == nil then
05        playerDebounces[Player.Name] = false
06    else
07end)
08 
09game.Players.PlayerRemoving:Connect(function(Player)
10    playerDebounces[Player.Name] = nil
11end)
12 
13game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(Player)
14    local existent = false
15    for i = 1, #playerDebounces do
View all 29 lines...

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
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 chess123mate 5873 — 5y
Ad

Answer this question