If you’re worried about people spamming remotes and don’t know any methods to combat this, this tutorial is for you! You should always have some sort of check on your remotes to validate each request. In this tutorial, you’ll learn how to stop RemoteEvent spam.
Note that this is just my way of doing this, so there are probably a lot of other methods people can do (for example, using os.time()
). More or less though, most methods are wrapped around the same concept: Tracking time since the last time the player fired the remote
Using tick()
tick()
is always a moving value, it’s the current number of seconds from the epoch (Jan 1 1970). But when you set something to tick(),
the value of that becomes tick() at the current time and doesn’t move anymore. I used to be really confused about this and thought it still moved. So, knowing that we could so something like this:
local var = tick() -- this becomes tick()'s current value at the time the variable is being set to it, and doesn't move
wait(3) -- wait for 3 seconds to pass, tick() would now be 3 seconds more because it's always moving
print(tick() - var) --> would be about 3
Implementing with RemoteEvents
Using the tick() method, we could now use this to act as a cooldown time for each player firing the remote.
When you make a game on ROBLOX with FE, you always need to be validating requests from the client on the server. So, obviously this is being done in a server script, where the server listens for the RemoteEvent.
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
-- run your code
end
end
Here, we basically track the time since the last time the player fired the remote by storing it in the table, and then check if the time passed since then is greater than the cooldown time. If it is, we grant access to the remote and set the index back to tick()
which resets it, because what that means is that you’re recording the time when the player fires the remote.
Conclusion
Just had to make a quick tutorial on this, so if it becomes of use to you, feel free to leave a like. Thanks for reading and have a good day!