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 4 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

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

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

1 answer

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

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
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 — 4y
Ad

Answer this question