Advertisement
SinisterMemories

[SH Post] Remote Event Debounces

Dec 16th, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | None | 0 0
  1. ##Your Question
  2. > Can someone please clarify why this error message occurs, what kind of affects it has on game scripts and which scripting practices are best to prevent this?
  3.  
  4. ##My Answer
  5. If you fire an event to much it will throttle. Meaning it will stop queuing incoming remote event requests from clients. This can be terrible at times. If you are firing a remote 1000 times a second and then suddenly a player can't play anymore because the server started throwing out their remote event request could be bad. Some easy ways to prevent this is to prevent the client from sending to many requests per minute and limiting themself. *(Do these debounce checks or whatever on the client)*
  6.  
  7. ###Example Code
  8. ~~~~~~~~~~~~~~~~~
  9. --// client example:
  10.  
  11. local replicatedStorage = game:GetService("ReplicatedStorage")
  12. local event = replicatedStorage:WaitForChild("Event")
  13.  
  14. local canFire = true
  15.  
  16. local function fireDebouncedEvent(event, ...)
  17. if canFire then
  18. canFire = false
  19.  
  20. event:FireServer(...)
  21.  
  22. wait(.125)
  23. canFire = true
  24.  
  25. return print("Successful event fire")
  26. end
  27.  
  28. return warn("Attempted to post to many requests")
  29. end
  30.  
  31. fireDebouncedEvent(event, "hello server")
  32. ~~~~~~~~~~~~~~~~~
  33.  
  34. Every `.125` seconds you can fire the event again with the passed args.
  35. You can also try doing checks on the server to see if a client is spamming your event such as logging how many requests they make a minute and if it to high then kick them.
  36.  
  37. ###Example Code 2
  38. ~~~~~~~~~~~~~~~~~
  39. local replicatedStorage = game:GetService("ReplicatedStorage")
  40. local players = game:GetService("Players")
  41.  
  42. local event = replicatedStorage:WaitForChild("Event")
  43.  
  44. --// settings
  45. local fires = {}
  46. local clearFires = true
  47. local maxRequestsPerMinute = 1000
  48.  
  49. local function handleOnServerEvent(player, ...) --// handle event
  50. if fires[player] <= maxRequestsPerMinute then
  51. fires[player] = fires[player] + 1
  52.  
  53. return print(player, "sent information: ", ...)
  54. end
  55.  
  56. return player:Kick("Spamming events")
  57. end
  58.  
  59. local function addToFiresTable(player) --// add new players to fires
  60. fires[player] = 0
  61. end
  62.  
  63. players.PlayerAdded:Connect(addToFiresTable)
  64. event.OnServerEvent:Connect(handleOnServerEvent)
  65.  
  66. while clearFires do --// clear every 60 seconds
  67. for i, player in next, fires do
  68. fires[player] = 0
  69. end
  70.  
  71. wait(60)
  72. end
  73. ~~~~~~~~~~~~~~~~~
  74.  
  75. ###Extra
  76. I kinda was to lazy to test any of the code I wrote on this answer. If it doesn't work it at least should be a good foundation on how to do something like this.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement