Firing Remote Events for All Other Players but One Player in constant time

I’m trying to have a projectile replicate to all player’s clients except the callers. I’m doing this with the simple architecture:

  1. player firing projectile, creates projectile locally (projectile is not replicated to other clients)
  2. player fires server event notifying of projectile creation/firing
  3. server replicates this projectile to all other player’s clients by firing another event, but it should only fire all players except the player firing the projectile (it is already on the client to make it seem lagless).

Originally, I was planning to just do a for loop:

for k, plr in ipairs(players:GetPlayers()) do
       if plr ~= caller then
            remote:FireClient(...)
       end
end

but that is not O(1) time; the number of comparisons grows linearly with number of players. Is there a better solution and is RemoteEvent:FireAll() O(1) time?

You could call FireAllClients(PlayerToIgnore) on the projectile creation event.

When the client recieves an event, it checks the value of PlayerToIgnore. If the client’s player == the PlayerToIgnore, do nothing. Otherwise, create a projectile.

I honestly don’t know. @Tiffblocks might know something about this.

5 Likes

Yeah I was thinking about doing that too, that’s why I was wondering if the :FireAll() was also O(1) time because I could just boolean check on client side. It would also be cleaner to script with.

1 Like

Well, this wouldn’t be true either. Just because it’s a single call from Lua, does not mean it’s O(1). That hypothetical method would still be O(n), but you’d just be looping through the players in the engine instead of in Lua.

This is kinda micro-optimization. I’d either do what you already have or use @Reshiram110’s solution.

5 Likes

The solution you already have is the cleanest. Don’t optimize things unless you can prove they are a bottleneck. I can’t imagine this being a bottleneck to any non-trivial project compared to if you were to do a single call. Moreover, for the alternative, you’d be sending extra events to clients that don’t need them (i.e. when plr == caller).

8 Likes

Exactly this - I try not to send anything to a client which doesn’t need to. What OP is doing now is perfectly fine, and it’s also what I’m doing for most things.