Is it a good idea to optimize :FireAllClients()?

I’ve noticed my game can be a bit slow and high ping at certain times. To try and fix this, I looked through my scripts to see if there is anything I could optimize. I noticed I was using :FireAllClients() very often, and my game is a combat game with many NPCs firing off client sided effects for combat, magic attacks, etc. Client-sided effects takes less stress off the server and allows for a more smoother experience.

I thought of a few different solutions to combat this:

  1. Distance Check: Instead of directly using :FireAllClients, I loop through every player in the game (max servers of 20), and check if their character’s position is within a certain render distance of the origin of the effect. This idea allows players across the map to not receive remote signals, and reduce the amount of remotes firing off. However, I wasn’t sure if constantly looping through every players in the game was a bad idea, so this led to the next idea.

  2. Client-sided Check: This idea is similar to the first one, but the server always fires :FireAllClients() and the client does a render distance check on their own. If the client out of the render distance, it doesn’t render the effect. This solves the looping through players issue, but still may lead to latency and lag issues due to the large amount of server signals.

Now my question is whether doing all of this is worth it or not. My goal is to lower the overall ping of the game (regardless of server location) and reduce latency and lag issues. Method 1 solves the issue of constant firing, but I’m not sure if looping through the player list makes up for it. Method 2 sounds good, but I’m not sure if Method 1 is a better version of it.

I appreciate any help. :grinning:

2 Likes

Method 1 is the best option as it reduces both client lag and networking lag.

With max server of 20 it should be fast though you might have to compare with let’s say 500 effect positions.

However you can consider using optimization techniques such as spatial hashing and octrees to handle these distance checks. (Personally not too knowledgeable on these techniques but know they exist) I’ll just put it here to also give you the same idea that there is some computer science knowledge which can optimize your game further.

3 Likes

I havent tested this, it might not work, but here’s an example:

Instead of doing :FireAllClients,

do a check:
for _,v in pairs(game.Players:GetPlayers()) do
— use if statements here
if blabla then
:FireClient(plr)
end
end

1 Like

In my opinion, absolutely what dthecoolest said. Sort of FireAllClientsWithinRadius.

The latter approach slightly unburdens the server at the cost of no improvement in ping times.

Latency can also be highly affected by the size of the sent info, so you may be able to do a lot by sending minimal information, maybe even serialized in your own way (1 → effect 1, 2 → effect 2, and so on). The utmost compression would most likely include byte strings and string.pack(), possibly queued and sent once at the end of the frame in one long string. I wouldn’t go in too complicated directions unless necessary.

String byte size is roughly equivalent to its length (“hello” = ~ 5 bytes). Numbers are similar but a bit different. (Size of short strings should be lower than of short numbers with the same amount of characters; the opposite applies to long strings and numbers of the same length.) Soft network bandwidth limit is 50 kB/s per player. + Blank remotes take about 9 bytes.

That’s my two cents, and the reminder about loops and any frequently called functions bound to RunService events.


@RobloxPlayer_9384 unfortunately I’m fairly certain individual firing to the same amount of players won’t show any improvement. Still sending a signal to each player, sending the same arguments, and calling multiple times instead of once.

3 Likes

Are you sure the cause of lag is actually what you think it is?

Try disabling the remote fires, and see if it prevents lag or not.
Important things (quotes I think, but I don’t remember of who) to keep in mind:

Premature optimization is the source of many issues.
Profile first, optimize later.

Once you’re sure the remote fires are causing lag, look into what data you’re sending. And see if you can reduce the amount of data you send, or send at lower intervals.

Since you have 20 player servers I assume your game’s map is pretty small? In that case, magnitude checks won’t help that much, as players will likely be close together. It’s also not very desirable if players can see far away, as they wouldn’t be able to see the effects.

1 Like