I have made a flag capture zone, everything works fine, the concern is performance.
The hitbox around the zone is cylinder so I just use magnitude checks in a loop which runs every 0.1 second on each player.
But in the game there can be many flags and near 100 players as well.
So lets say there are 10 scripts running magnitude checks on 100 players’ HumanoidRootPart every 0.1 seconds, will it be performance inefficent? I do have noticed script usage rising up to 5% on a server with 40-50 players, and I am sure it has the potential to rise up to 10%-15%.
Now this will introduce even more lag on the already laggy server (due to the combat scripts, but that is
a whole new topic).
Is there any efficient way of doing this? I have tried doing .Touched events but the problem is it fires once per zone touched, that means to start the capture of the zone you will have to re-enter the zone.
Forget about :GetTouchingParts, it caused even more lag.
I have thought about doing raycasting but I doubt it will be anymore efficient, or am I wrong?
I do far more intensive logic than this every frame, and the server doesn’t blink an eye…
If you’re doing something like:
--Every 0.1 seconds
for _,flag in pairs(flags) do
for key,player in pairs(players) do
local dist = (flag.pos - GetPlayerPos(player)).Magnitude
if (dist < radius) then
CapFlag(flag)
end
end
end
Then there is no reason why you couldn’t do this for 100 players and 10 flags. I recommend you check the script performance tab (F9) and see where your performance is going?
Do you have any example of the “far more intensive logic”. Maybe I am overthinking this way too much, an example with how much script % it uses will confirm it.
I have seen script usage rise to up to 5%, but I am more worried about the total script usage of the whole server.
5% usage, when added with the script usage of other scripts can cause lag. Or maybe I am still overthinking this.
Yeah I will be optimizing the other scripts too, but first I want to start small with the capture point and work my way up to the bigger and more complex scripts (plus I didn’t write the scripts, it was an ex-developer, if it was mine I would have already optimized them.).
Maybe it could be a memory rise instead of the CPU in my server. Because there is a memory leak somewhere (that is a whole new topic because the current combat code is not at all organized).
I have seen memory rise up to 4 gb and server starts experiencing lag. Maybe I have mistaken that for the CPU usage?
Yeah. I’d look over your server stats and try and figure out where you’re actually slowed down. You might be maxed on CPU usage, but I noticed lots of players tends to either max out the replication rate or the bandwidth itself. Check your server KB/s out?
Are you sure that the lag is from the magnitude checks? If you’ve disabled other things and the lag is still there, consider doing those magnitude checks less often and using rootless distance checks instead of magnitude checks (the second point shouldn’t make much of a difference, but i’ve seen a few people doing it)
Raycasts are relatively cheap. Depending on length, they are definitely faster than a magnitude check.
@OP
You can probably use region3 or some similar method, just use it on the flags instead, and probably make the loop run slower. It should probably decrease the script usage.