How to detect when an npc touches a certain part

I want an npc to move to two parts one after another.

This is what the code looks like so far:

local dummy = script.Parent
local middle = game.Workspace.Spawn1Middle
local End = game.Workspace.Spawn1End

dummy.Humanoid:MoveTo(middle.Position)
--detect if humanoid touched, if so, go to the end position, otherwise keep walking
dummy.Humanoid:MoveTo(End.Position)

I don’t know how to detect when the npc touches the middle part.

I tried doing dummy.Humanoid:Touched but I didn’t know how to implement it.

1 Like

imagine if you make a post and noone responds, couldnt be me :skull:

local dummy = script.Parent
local middle = game.Workspace.Spawn1Middle
local End = game.Workspace.Spawn1End

dummy.Humanoid:MoveTo(middle.Position)

middle.Touched:Connect(function(Hit)
	if Hit.Parent:FindFirstChild("Humanoid") and Hit.Parent.Name == dummy.Name then 
		dummy.Humanoid:MoveTo(End.Position)
	end
end)


Have you seen the MoveToFinished event?
https://create.roblox.com/docs/reference/engine/classes/Humanoid#MoveToFinished

How would i implement that?

I tried doing this but it didnt work:

local dummy = script.Parent
local middle = game.Workspace.Spawn1Middle
local End = game.Workspace.Spawn1End

dummy.Humanoid:MoveTo(middle.Position)
if dummy.Humanoid.MoveToFinished(middle.Position) then
	task.wait(1.5)
	dummy.Humanoid:MoveTo(End.Position)
end
note to @soggyfeet13

@CZXPEK your script didnt work

That is me rn. I feel pain lmao

1 Like

Instead of detecting if NPC have touched or no, you can wait for NPC to reach middle part. Here is the script anyways

local dummy = script.Parent
local middle = game.Workspace.Spawn1Middle
local End = game.Workspace.Spawn1End
local v1 = false

dummy.Humanoid:MoveTo(middle.Position)
spawn(function()
while wait(1) do
if (dummy.HumanoidRootPart.Position - middle.Position).Magnitude <= 10 then
v1 = true
end
end
end)
while not v1 do
    wait()
end
dummy.Humanoid:MoveTo(End.Position)

MoveToFinished isn’t a method, but rather an event. An implemenation of this would be:

local dummy = script.Parent
local middle = game.Workspace.Spawn1Middle
local End = game.Workspace.Spawn1End

dummy.Humanoid:MoveTo(middle.Position)
dummy.Humanoid.MoveToFinished:Wait()
task.wait(1.5)
dummy.Humanoid:MoveTo(End.Position)
1 Like