Dealing with .Touched events and their inevitable delay buildup in my game

I want to make use of the .Touched function, but when in use with the rest of my game it eventually builds up to lag for each touch. Each previous touch function set up in the same way disconnects when it is no longer needed.

The issue is that lag generally increases for each touched event but I have no method for tracing the issue. The touched code does not have anything which makes it yield.

I have tried adding a debounce so that the game can still process any ongoing touched function if it’s still working on that so it doesn’t get jammed up.

Anybody else have issue with .Touched events eventually increasing in their delay of operation, and what have you tried to fix it? I feel like it’s a more generalised issue rather than .Touch, so any input is appreciated.

4 Likes

How many touched events are you using?
Ive used quite a few touched events at once and I cant say Ive experienced anything like this with touched or any other roblox event
Perhaps there could be an alternative to touched, maybe they could be grouped together, or the connection and disconnection of the events is causing the issue

1 for a button in the games map.
1 for deleting parts in an unauthorised area
1 for triggering a mission in a map that spawns by chance.
1 for each players humanoid root part that gets disconnected on death
1 for each players humanoid that gets disconnected on death

At the start of testing sessions there is no lag. As the game loops more times the issue becomes more prominent and the lag only gets worse. I don’t know if this is an issue with leaks for Touched, don’t those events disconnect when parts that it’s connected to no longer exists?

1 Like

Do you disconnect those events after they are done, just to clean up if its an unnecessary event since unnecessary events need to be cleaned up after they are used

No. It is my understanding that events get automatically disconnected if the object it is connected to no longer exists.

Roblox Dev WIki:

Always call Disconnect() when a connection is no longer needed. Forgetting to do so can cause your game to use more resources than necessary. Note, however, that this may not always be necessary; when an object is destroyed, all connections to that object’s events are disconnected automatically.

Based on this all my previously set up touch functions should be disconnecting automatically.

hmm sure but if one signals the event and other players firing that event too it would cause lag

e.g 52 people touched a button order by order

I’d probably guess it is something other than touched events. Unless you are creating lots of them unnecessarily, they shouldnt be the problem. They clean up on their own.

1 Like

I’ve personally have not had any issues with .Touched, however, in your game if you have 40 people touch a button at once then of course lag will build up considering that event is being fired multiple times.

Are you completely destorying the parts or are they just hidden (i.e. parent set to nil or made non-collidable/transparency set to 1)?

If I were you, I would go ahead and use Disconnect() whether or not the object is being destroyed, it could help when doing it manually. Though I am unsure if it will or will not make a substantial difference, it’s worth a try to be honest

On the Instance:Destroy() page, it says disconnects all connections and calls Destroy on all children. This function is the correct way to dispose of objects that are no longer required., So your issue seems strange, you can try manually disconnecting all the events though and test to see if it still lags, collect every connection in a table then disconnect them all at the end of a round.

I’m unsure if the problem is actually with the .Touched event itself or just it being mass fired when a lot of players touch a button, so I’ll provide solutions for both cases:

If the problem is with .Touched event being laggy then you could try switching to Region3, since your buttons use Hitbox parts you can use this module to do create a rotated Region3 from a part.

If the problem is with the connection then you could try Magnitude checking, so every RunService.Stepped you can store a table of magnitudes from each player to the current active button, whenever the Touched event is called it will set a variable to true, which will make the next RunService.Stepped loop check if the nearest player to the button is in correct range, activate the button functions and set that variable back to false.

I’m unsure if the last solution will work since I cannot test with 40 players.

2 Likes

if your server-side lags bc touch event just make touch event(you can use fireallclients(data) ) on client-side then fire what you want back to server-side btw bc it uses remoteevent so hacker can exploit it,you can fix it by add sanity checks example if data1 == " " and data2:IsA(Class) etc. else kick or your ban system Make sure data from client is correct

the problem about this way is after player died or new player joins it won’t work so you might fire it again