I’m trying to have a projectile replicate to all player’s clients except the callers. I’m doing this with the simple architecture:
player firing projectile, creates projectile locally (projectile is not replicated to other clients)
player fires server event notifying of projectile creation/firing
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
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?
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.
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).