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

:MoveTo() works when it's moving the model, but not the humanoid?

In a script I'm working on, an NPC should move to the nearest player. However, in the current script I have, the NPC doesn't move at all. But, if I change line 42 to:

script.Parent:MoveTo(target.Position,target)

it directly teleports the NPC to the target player. But with the current line, the NPC doesn't gradually move towards the player. What am I doing wrong?

function findNearestTorso(pos)
	local list = game.Workspace:children()
	local torso = nil
	local dist = 10000
	local temp = nil
	local human = nil
	local temp2 = nil
	

	for x = 1, #list do

		temp2 = list[x]

		if (temp2.className == "Model") and (temp2 ~= script.Parent) then
			temp = temp2:findFirstChild("HumanoidRootPart")
			human = temp2:findFirstChild("Humanoid")
			
			if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
				
				if (temp.Position - pos).magnitude < dist then
					
					torso = temp
					dist = (temp.Position - pos).magnitude
				end
			end
			
		end
	end
	return torso
end


while true do
	wait(1)
	local target = findNearestTorso(script.Parent.HumanoidRootPart.Position)
	if target ~= nil then
		script.Parent.Humanoid:MoveTo(target.Position,target) --this line
		print("moveto")
	end
end

Thanks!

4 comments

0

You have to make the NPC look at the target, then make it move about 1 unit forward towards the target. I forgot how to do this since I don't do it but try something out!

0

:MoveTo() is basically a teleport for model's like changing the position.

0

if you want the npc to go to somewhere by walking use Humanoid.WalkToPart or Humanoid.WalkToPoint

0

Your code works. I have tested it with a def rig. Did you anchor any parts of the npc?

1 answer

:MoveTo() is basically a teleport for models like changing the position. if you want the NPC to go somewhere by walking use Humanoid.WalkToPart or Humanoid.WalkToPoint

1 comment

0

Wrong

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