Performance increase for large quantities of touch objects

I’m trying to create a collection system where player collects eggs, however it is incredibly slow. My ping and framerate. It’s af is there’s like a max touch limit per second or something.


There’s 150 eggs there. It should collect all of them instantly and have them disappear, but instead they stay on screen and can take 2-3 seconds before disappearing. The code has no yields, and is a seperate Touched connection for each individual egg model

Drop.Touched:Connect(function(hit)
		local Character = hit.Parent
		if not Character then
			return
		end
		
		local Player = Players:GetPlayerFromCharacter(Character)
		if not Player then
			return
		end
		
		if Player ~= self.Tycoon.Owner then
			return
		end
		
		local Data = DataService:Get(self.Tycoon.Owner)
		if not Data then
			return
		end
		-- Add to eggs
		if Data.Eggs[Drop.Name] then
			Data.Eggs[Drop.Name] += 1
		else
			Data.Eggs[Drop.Name] = 1
		end
		
		Drop.Parent = workspace.EggCache
		Drop.Anchored = true
		Drop.Position = Vector3.new(10000, 10000, 10000)
	end)
1 Like

Maybe because the Touched event is firing too much for those legs

Merge eggs

You should reduce the number of eggs by merging the eggs together

Like when an egg is close to another egg, The other egg gets deleted and the egg gets bigger and will double its reward

It will help reduce latency and performance

1 Like

Regardless of location, just to get variables out of the way, anchor and position the object first before parenting to the location you chosen.

Secondly, try getting rid of the data checks (For Now) and see if that improves anything, if it does, you may have to rethink your data checking OR just rethink the way in which the eggs spawn/are collected. @BirdieI90 gives a good suggestion on combining the eggs to reduce the concentration of parts in a given area, of which touch events struggle to handle.

Note:
Touch events can be laggy in large concentrations due to them activating touch events on each other.

1 Like

what Ping said is correct

also

is this touched stuff on the server side? just asking
because spawning things using the server will [of course] cause alot of lag, and touched events may have some delay

i would rather use touched event on client-side, and each time he touches he will just fire a remote event, idk if this would be perfomant, since it will only send to the server the ‘‘egg’’ tha the player touched, and btw, create some sort of Zone, so, only if the player is inside that zone he will be awarded the Egg from the Remote event that he just fired,

it will be a spam of remote events, but

Quote from someone in devforum

Remote queue exhaustion is per player; it isn’t a cumulative “pot” that each player takes from. An exploiter who sends 20k requests will only ever affect their own gameplay. This will simply cause the server to start dropping requests for that specific player and the game will function normally for all the other players. If you’re worried about your datastore being accessed too much, then simply create a cache of the player’s data.

TL;DR
You don’t need to make your own queue if you’re worried about excessive remote spamming since it’s handled automatically, but you’ll obviously still need to debounce for purposes of cooldowns, sanity checks etc…

Side Note:
Some Denial of Service attacks target expensive operations on the server. An example avenue of attack could be: an open remote that fires a very long ray, then checks for parts in a large Region3 etc… The exploiter spams that remote to cause the server to use as many of its resources as possible in an attempt to cause lag, a crash, or a buffer overflow. An exploiter with two+ accounts would be a DDoS attack. In short: debounce first then perform any operations after your sanity checks.

2 Likes

You could try putting the Touched event somewhere on the character instead of the eggs. This way you have way less events.

3 Likes

Probably because Touched Event firing too much causing lag spikes, maybe you could add the eggs inside a table and use ZonePlus to detect if the Player entered the zone where you would Collect the items and if they enter it give them the eggs?

1 Like

First of all, using a Touched event is not a good idea whatsoever.
Check if the player is near to the dropper and if that’s the case - run a spatial query.

I suggest workspace:GetPartBoundsInRadius.