Basically I have made a game that contains thousands of tiny cylindrical parts. Each of those parts contain a touch event that would award the person who touches it with one point. The parts are not anchored and are raining down from all over the places.
The game works fine when there are one to two players in the game. But when more players joined (maybe 4 to 5 players in the game), things start to lag badly.
I’m just wondering if this is normal for a Roblox game with this many parts with touch events in them or if anyone has any suggestion on what I should be doing to improve the game’s performance?
I understand that one way to improve performance is to reduce the number of parts fallen from the sky, but is there a way to keep all those parts in the game and improve performance at the same time?
You could give every player a touched event (plr.Character.Humanoid.Touched) instead of each part.
Then when it fires check if the touched part is a collectible cylinder instead of checking whether it’s from a player character.
That should increase performance by a lot, but having thousands of physically simulated parts is going to lag no matter what you do.
The only thing I’m worried about there is wouldn’t that record dozens of touches for every second that the player is standing or moving on a platform? Wouldn’t that takes up even more performance time?
It’s a good idea for coding design, but would that help the performance of that game? You would still need to add a touch event to all the parts that are tagged.
In Roblox all events always fire. Once one does it goes through it’s list of connections and runs each of them. (also touch only fires when a collision starts, not every frame while it’s touching)
So it’s either having thousands of connections or 10
No, you can do CollectionService:HasTag() to check that a touched part (from a touch event on a player’s humanoid/character) is a cylinder that can be collected and is not just the ground or another player.
I can use collection service or I can just add an attribute to each part, both would work the same or does one takes up more processing time than the other? I may use an attribute instead because I can indicate how many points each different part is worth. With collection service, I can use different tags but I would need to compare the tag each time and that slow down the system?
You can do both, maybe make different tags for different amount of points. I think the performance hit of comparing tags or reading an attribute is negligible. So you can choose what you like best.
It looks like you’re trying to make this game: Lag Test 2021 - Roblox
Not only do all these parts cause lag, but in my opinion it’s also hard to look at. It doesn’t make sense why there are so many flying parts. Maybe consider using a lot less parts, or finding a new concept for what you’re trying to accomplish.
I think you’re missing the point of this discussion. The point is how do we improve performance of a game where there are tens of thousands of touchable parts. @ketrab2004 made a very good point to place the touch event inside the characters which I will try it out when I get a chance. Another person (not from this forum) suggested that I use square parts as opposed to cylindrical parts, I will also test out that theory when I get a chance.
Changing the cylinders to cubes will make the physics cheaper, but looking at the video I think it slows down with more players because the server struggles to replicate all those parts to each player.
You could maybe try anchoring parts that have landed or replace the physics with raycasts. If that doesn’t work you will have to decrease the amount of parts I’m afraid.
I was just curious as to why the game lags when more players joined, your explanation does make perfect sense. The lag is actually not all bad, it does create an unintended nice effect where everything is hanging in the air, kinda like a snowstorm. But I will definitely try everything suggested to see how it all turns out.
I have made the change you recommended to assign the touch event to each character instead of the parts. It works great, but I will know for sure when there are more players joining into the game. Just knowing that now there are thousands of connections less than before makes me feel great!
Thank you so much for all your help and for that great advice!