I've created a very basic NPC - rigged, animated, animation works fine (bit amateur, but works.) All necessary parts are present and welded - HumanoidRootPart, etc. Trouble is I can't get the NPC to follow my player. It walks around aimlessly for a while, then does a lemming off the edge of the baseplate!? I'm getting no errors in output. Used 'print' to add checkpoints to the script. All seems good. I'm trying to tweak/modify the same 'classic' script that comes with the Zombie model. (Yes, I know, it's better to script it all from scratch, but variations of this same script seem to be everywhere - why not use? :-)) Thanks in advance!

Here's the script:

local larm = script.Parent:FindFirstChild("HumanoidRootPart")
local rarm = script.Parent:FindFirstChild("HumanoidRootPart")

wait(0.1)

print(script.Parent.Name)
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.Name == script.Parent.Name) 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
	print("Crappppppp")
	return torso
end

while true do
	wait(1)
	local target = findNearestTorso(script.Parent.HumanoidRootPart.Position)
	if target ~= nil then
		script.Parent:MoveTo(target.Position, target)
	end

end

3 comments

0

humanoid:MoveTo(Part.Position)

0

that may work

0

Thanks, but just tried that. No joy - NPC still 'wandering'.