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

Movement Script doesn't move the NPC?

In this script, it detects a player, then moves an NPC to it. Everything seems to be working, but the NPC doesn't move. What am I doing wrong?

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

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]
		print(temp2)
		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)
		print("moveto")
	end
end

Thanks!

1 answer

Using standard pathfinding would work but would have some flaws. If you want to detect when a player joins a game a simple

[npcname].Humanoid:MoveTo(game.Players.[playername or localplayer])

but if you want it to move when the player gets a certain distance to it then a testfor a humanoid within a certain stud radius would work fine too.

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