Hello, I am wondering how I could optimize this zombie module to make it so there less lag.
Right now if you have more then about 10 zombies the game running 13 FPS on a good PC.
The zombies are really simple models just with a body and a humanoid so I don’t know why this is.
Currently everything is CanCollide to make it so you get squished and you can’t just keep running away.
Oh there is code inside. It’s for constantly updating the Zombies position well using pathfinding service. I read that using heartbeat is better then while loops because it’s connecting to the same thing. Not a separate loop every time. I just didn’t want to show the whole script.
The script is not supposed to work because the findNearestTarget() function is empty, and the RunService.Heartbeat event is connected to an empty anonymous function. Jokes aside, I can’t exactly determine what could cause the lag or the small amount of frames per second. However, there’s an unnecessary task.spawn() function because there’s no endless loop inside its parameter function. Additionally, I don’t understand why you are checking if the script is running on the server side, but that surely isn’t the cause of the lag. Lastly, as far as I know, RunService.Heartbeat is becoming deprecated, and the RunService.PostSimulation event is a newer alternative to it. That’s all what I noticed, correct me if I’m wrong with something.
Oh, and I forgot to mention the most probable cause of the lag, which is the usage of PathfindingService. As far as I know, it uses complex systems that may lag the game, especially when spammed. PathfindingService is a good solution for a singular humanoids that have an important role in the game. If you want to create a player-chasing army of zombies, I’d advise using the Humanoid:MoveTo() function with waypoints and recursion.
Yeah, I’m doing that already. Also nothing in the code is empty, I just didn’t want to show all of the code publicly because somebody could take it. And I am using humanoid:MoveTo() But how can I ultilize pathfinding service if I have to do it so often? The zombies lag if I do it every say 0.01 seconds.
I think the reason why they are so laggy is because there is a heartbeat function called every time a zombie is created. But I don’t know what an alternative to this is. Does anyone have an idea?
I guess it kinda doesn’t, but it still has a use GetService Returns a service with the class name requested . When called with the name of a service (such as Debris) it will return the instance of that service. If the service does not yet exist it will be created and the new service is returned.
Well, the best way is to not create the zombies on the server side, rather store their data (positional data + id) from the server and send it over to the client periodically (1/10th of a second at best; avoid heartbeat) after doing your pathfinding. And from the client side if the id of the zombie isn’t already registered create a zombie under its name and smooth out the movement using lerps.
They could for example open the local script and edit it so the zombie has no health or doesn’t move. It would be an insane unfair advantage. And also setting that system up would be a pain.
It is a round based zombie game where zombies spawn with animations and using pathfinding they follow the player. I am trying to make it more optimized and less laggy.
You shouldn’t make a heartbeat event per zombie. Instead, run all the zombie AI’s in one heartbeat event. The task.spawn is unnecessary, as heartbeat runs in the background anyway.
Actually then my system would work just fine, since zombies are handled client side, changed made to one zombie by a particular client won’t replicate to others.
Since all the pathfinding and etc are done on the server, the server can easily check if the distance between a zombie and a player is less than a certain amount and kill the player accordingly. Keep in mind that the server always handles all the logic and the client just renders the logic i.e handles the visuals