I have a Tower Defense game where enemies move along a track and then disappear once they are killed or have reached the exit. The movement function, when broken down, basically looks like this; the function repeats a humanoid:MoveTo() function for the current Waypoint and once its reached it will move onto the next waypoint. (headingto is if a mob is spawned halfway through the track via another mob, and waypointstype are for different paths; example Waypoints1 and Waypoints2, so don’t worry about that part)
local waypoints = map.EnemyPaths[tostring(mob.WaypointsType.Value)]
local humanoid = mob:WaitForChild("Humanoid")
for waypoint = headingto, #waypoints:GetChildren() do
if mob:FindFirstChild("MovingTo") then
mob.MovingTo.Value = waypoint
repeat
humanoid:MoveTo(waypoints[waypoint].Position)
local reached = humanoid.MoveToFinished:Wait()
until reached
end
end
The problem lies when people are actually put into a game and the mobs actually start spawning. I have towers in the game that create summons that walk on the path. Rarely however, people experience what I at first assumed to be lag, where the summons would barely move, and therefore the summon tower spawned quite a lot of them, causing lag. These summons are meant to be fast moving so there shouldn’t be this many out at a time. Pictured below are some examples. Unfortunately I do not have video of this as of this time. Notably, Mobs also seem to “overshoot” where their MoveToFinished is supposed to end, because they will end up falling off the path. Also to note, nothing in DevConsole logs shows up.
My theory at first was that roblox sometimes puts you in a low latency server where everything runs much more slowly, both summons and just any function in general like towers attacking and whatnot (since my game isn’t a neccessarily popular one; i read that lower played games get more low quality servers), because I myself have experienced this behavior, but I’m wondering if it’s something more than that or something I can fix. Any help would be GREATLY appreciated!
It sounds like the issue may be related to the performance of the MoveTo() function, which could be affected by the number of summons and other mobs currently in the game. Here are a few potential solutions to improve performance:
Reduce the number of active mobs and summons in the game at any given time. You mentioned that the issue occurs when the summon tower spawns a lot of summons, so limiting the number of summons that can be active at once may help improve performance.
Optimize the MoveTo() function by reducing the frequency with which it is called. You could try increasing the distance between waypoints or reducing the speed of the mobs and summons so that they don’t need to move as often.
Use a custom pathfinding solution instead of the MoveTo() function. This would require more coding, but may be more efficient and give you greater control over the movement of the mobs and summons.
Consider using a plugin like Pathfinding or NavigationMesh to help optimize movement along the path. These plugins provide more advanced pathfinding algorithms and can help improve performance.
It may also be helpful to use the Profiler to identify which parts of your code are taking the most time to execute, and focus on optimizing those areas.