Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Unusual Move To Behaviour?

I've been trying to make an NPC move towards two mapped points using MoveTo(). It works fine though instead of going straight to the appropriate point it goes a few steps before it.

Since I'm not the most elegant person when it comes to explaining something I'll just show the DIY code (which to me seems fine?).

local list = {"P1", "P2", "P3", "P4", "P5"}
local npc = game.Workspace.NPC.Humanoid

local i = 1

npc:MoveTo(workspace.Nodes:FindFirstChild(list[i]).Position)
i = i + 1
npc.MoveToFinished:wait()
npc:MoveTo(workspace.Nodes:FindFirstChild(list[i]).Position)

Any help would be much appreciated.

1 answer

✓ Accepted answer

Your code is fine; this is a "feature". To work around this, you can either adjust the points so that they are a little bit beyond where you want the NPC to stop, or you can do that automatically in your loop:

local list = {"P1", "P2", "P3", "P4", "P5"}
local npc = game.Workspace.NPC.Humanoid
local head = npc.Parent.Head
local gap = 2 -- change this to however many studs the NPC stops away from your destination

for i = 1, #list do
	local point = workspace.Nodes[list[i]].Position
	local dir = (point - head.Position).unit -- the direction the NPC will move to get to your point
	point = point + dir * gap -- this compensates for the "feature"
	npc:MoveTo(point)
	npc:MoveToFinished:Wait()
end

This discussion is preserved as it was. New answers, comments, and votes are closed.