MoveTo In repeat loops, inefficient?

So recently for my zombie controller, I started looking for examples to make it more efficient, during that I saw people doing what I shown below, after the path was created to the player, and the zombie starts chasing the player.

repeat 
humanoid:MoveTo(Character.Root.Position) 
wait() 
until CONDITION

Since :MoveTo() creates a new thread when called aren’t they potentionally creating a thread every iteration, which leads to the server crash overtime. Does roblox automatically kill the moveto thread when its called a second time? The reason I’m confused is a lot of controller script’s do this.

1 Like

Where in the docs does it say it creates a new thread each time?

My understanding, as per the docs, is that this simply changes the WalkToPoint property, and based on that understanding, continuing to change this property again and again will simply update where the humanoid is walking to, rather than creatings tons of competing threads all trying to walk to different places.

By all means correct me if you have more authoritative info - I’m just using the docs.

From the docs:

This function causes the Humanoid to attempt to walk to the given location by setting the Humanoid.WalkToPoint and Humanoid.WalkToPart properties.

It’s just changing a property. The script you posted is fine, performance-wise.

Oh, so everytime your creating the MoveTo thread it just changes the property of the destination/ WalkTo property

I don’t believe you are creating a “MoveTo thread”. Where are you getting this idea from that it is anything to do with threads?

But yes it just changes the properties as far as I’m aware. The logic of actually moving the humanoid happens elsewhere by Roblox.

1 Like

I think I misunderstood the Humanoid:MoveTo() function then, so do you think it’s safe/faster to use :MoveTo(), then writing a custom moving function?

Just as an additional consideration, you might not actually want to use wait().

I think it’s safer, easier, and probably faster too as it allows Roblox to take advantage of C-sided logic instead of doing it in Lua.

No issues with your code snippet as far as I’m aware.

You can use the microprofiler in studio on the server if you want to double check it isn’t delaying subsequent frames, but I think you’ll be fine.

2 Likes

Another note, for this scenario you might want to consider using the event which runs when MoveTo finishes running:

humanoid.MoveToFinished:Wait()

It gives your yield more purpose.

1 Like

Oh ok, so humanoid.MoveToFinished:Wait(), is actually bad for this scenerio, the whole point of the Repeat loop was to update the zombies walkDestiny, if the player moves ect. If I yielded the code if the player moved the zombie would finish walking to the old destiny and then walk to the new destiny, which is bad.

This isn’t the pathfinding code, it’s the code which runs once the player is in line of sight of the zombie.