Npc Humanoid:MoveTo function stops/not working sometimes

Hello!

While developing my game I ran into an issue, it’s not game breaking but it ruins the experience to say so. Please read the whole post to understand the situation.

This part is gonna be long.

Context

For some context, I am making a FNAF fangame. Basically if this helps in any ways, this is how I do basic « movements »

game.ServerStorage.NextNPC:Clone().Parent = game.Workspace
game.Workspace.PreviousNPC:Destroy()

This works perfectly fine.

Now the Animate script the npcs have is the normal one, nothing really important that was changed except me removing the emote/chat part of the script.

How it works is basically, (callback to the « movement ») when one has been cloned, it goes to a part using MoveTo and MoveToFinished. When for example, I want to test the path, I just put one from ServerStorage, put it in Workspace Then press Run in studio to see how it looks. When I do that everything works fine.
MoveTo script (based on memory):

local sun = script.Parent --sun is just a name of the character
local hum = sun:WaitForChild("Humanoid")

hum:MoveTo(PartA.Position)
hum.MoveToFinished:Wait()
--goes on

The problem itself:

Problem

Let’s say I’m play testing my game. Sometimes, some NPCs will stop after being on a part. So for example, one npc has 3 parts, a,b and c. When sometimes it will just go to A like normal, then do nothing. Just stand there. In my OutPut I don’t think there’s any errors.

I could share the script in its entire form (to get names).
It seems some NPCs will perfectly do their MoveTo function. While some, sometimes will just stop at the first part or do the function perfectly.

I can share video footage, (while writing this I am in a bit of a rush).
If there’s any questions or forgotten details, please let me know.

1 Like

Specific Script:

local sun = script.Parent

local humanoid = sun.Humanoid

humanoid:MoveTo(workspace.AtDeskA.Position)
humanoid.MoveToFinished:Wait()

humanoid:MoveTo(workspace.AtDesk.Position)
humanoid.MoveToFinished:Wait()
humanoid:MoveTo(workspace.AtDesk2.Position)
	humanoid.MoveToFinished:Wait()

I’d suggest adding print() statements? If the script sometimes work, then maybe the Humanoid.MoveToFinished event isn’t being detected just yet? You could also add a timeout after a certain while to teleport the NPC instead if it hasn’t reached its desired goal

1 Like

Maybe just change it to wait(number)

Can we see the whole script?

Sure:

local sun = script.Parent
local humanoid = sun.Humanoid

humanoid:MoveTo(workspace.AtDeskA.Position)
humanoid.MoveToFinished:Wait()

humanoid:MoveTo(workspace.AtDesk.Position)
humanoid.MoveToFinished:Wait()
humanoid:MoveTo(workspace.AtDesk2.Position)
	humanoid.MoveToFinished:Wait()

Well MoveToFinished only works if the humanoid has the exact position I think. If the npc stops moving check if the Torso position X and Z axis is the same as the goal position. I’m not sure though.

1 Like

Oooooh, the first part is different from the 2 others. I’ll try that and come with an answer.

So I playtested my games 2 times, it worked. Maybe the problem isn’t solved yet but we know how to fix it. @JackscarIitt @B2ontwitch
If it still doesn’t work, I can do a Touched:Wait() and cancel the MoveTo and do the next function

Good idea, but the problem is it can fire if something
else touches. I would do

local TouchedWait = part.Touched:Connect(function(object)

if object.Parent == npc then

TouchedWait:Disconnect()

else

part.Touched:Wait()

end
end)

part.Touched:Wait()

1 Like

It happened again.

I’ll try something like this:

hum:MoveTo(AtDeskA.Position)
AtDeskA.Touched:Connect(function(object)
if object:FindFirstFhild("Humanoid") then --basically checking for a Humanoid
--Rest of the code
end
end)

@JackscarIitt @B2ontwitch

Wait something weird happened. MY PROGRESS WASN’T SAVED

Nevermind, SOME of it was saved for some weird reason.

Use magnitude condition checking instead of hum.MoveToFinished:Wait(). Maybe that’ll solve the problem.

By that I mean
repeat wait() until (PrimaryPart.Position - Desk.Position).Magnitude < 2.
Adjust the 2 a bit to your own liking.

2 Likes

I’ve never used magnitude, could you briefly explain it or send an article?

(Part1.Position - Part2.Position).Magnitude ==> Calculates the distance between 2 parts in studs.

1 Like

magnitude is the distance between 2 objects
for example my character’s head and a random part
to find the difference of studs between those 2 objects what we can do is :
game.Players.PlayerAdded:Connect(function(player)
local part = workspace.Part
local distance = (Part.Position - player.Character.Head.Position).magnitude
print(distance)
end)
The result will be coming in studs

1 Like

I’ll try this and other solutions. I’ll comeback with answers and let’s hope it works. @Zohair_028 @Lin27 @JackscarIitt @B2ontwitch

So @JackscarIitt @Zohair_028 @B2ontwitch @Lin27, I did a touch event. So if there’s any problems I’ll comeback.
Script:

local sun = script.Parent

local humanoid = sun.Humanoid

humanoid:MoveTo(workspace.AtDeskA.Position)
game.Workspace.AtDeskA.Touched:Connect(function(hit)
	if (hit.Parent:FindFirstChild("Humanoid") ~= nil) then
		print(workspace.AtDeskA)

		humanoid:MoveTo(workspace.AtDesk.Position)
		humanoid.MoveToFinished:Wait()
		humanoid:MoveTo(workspace.AtDesk2.Position)
		humanoid.MoveToFinished:Wait() 

	end
end)

One thing I see is that you aren’t assigning a debounce to “AtDeskA” when it gets touched, this could be why perhaps?

Try doing this instead:

local sun = script.Parent
local DB = false
local humanoid = sun.Humanoid

humanoid:MoveTo(workspace.AtDeskA.Position)
game.Workspace.AtDeskA.Touched:Connect(function(hit)
	if (hit.Parent:FindFirstChild("Humanoid")) and not DB then
        DB = true
		print(workspace.AtDeskA)

		humanoid:MoveTo(workspace.AtDesk.Position)
		humanoid.MoveToFinished:Wait()
		humanoid:MoveTo(workspace.AtDesk2.Position)
		humanoid.MoveToFinished:Wait() 
        DB = false
	end 
end)

:MoveTo() only moves the character to face one way then keep going that way. It doesn’t detect obstacles. You should be using PathfindingService.