How do I wait until a function has finished running?

npc.Humanoid:MoveTo((plr.Character.HumanoidRootPart.CFrame * CFrame.new(0,0,-5)).p)
		npc.HumanoidRootPart.Anchored = true

the following code works
but, the issue is that it anchors it immediately…
is there any way for me to wait until the moveto() function has finished?

or do i have to do some dumb thing like make a variable for the new walktopoint after its changed by moveto and then repeat wait() until they reach the location

npc.Humanoid.MoveToFinished:Wait()

There’s a builtin event for that

what does :wait() do here
ive never seen it used like that

:Wait() will create a yield until that event is triggered.

oh thats very helpful
thanks lol

Wait is one of the methods of an event, what this does is that it waits for the event to fire, aka when the MoveTo as finished either by it reaching its destination or if the function timed out, this stops the thread so no code below it will continue till the event fires. Afterwards, it also returns anything that the event passes. For example, let’s say you have this

local character = player.Character or player.CharacterAdded:Wait()

If the player’s character doesn’t exist by the time it reaches that variable, it’ll wait for the CharacterAdded event to fire and then passes to the variable the character that was created

3 Likes