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

How would I make this script chase an NPC rather than a player?

I have this script for an alien and it works fine for chasing players, but I'm lost as to how to change it to chase the nearest NPC (a dummy in my case). Any help?

local larm = script.Parent:FindFirstChild("Left Arm")
local rarm = script.Parent:FindFirstChild("Right Arm")

function findNearestTorso(pos)
	local list = game.Workspace:children()
	local torso = nil
	local dist = 1000
	local temp = nil
	local human = nil
	local temp2 = nil
	for x = 1, #list do
		temp2 = list[x]

			temp = temp2:findFirstChild("Torso")
			human = temp2:findFirstChild("Humanoid")
			if human ~= nil then
				if (temp.Position - pos).magnitude < dist then
					torso = temp
					dist = (temp.Position - pos).magnitude
				end
			end
		
	end
	return torso
end

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

1 answer

put something unique inside of your NPC, because all living things in roblox have Torsos. you need something different to distinguish them from. you could add in an object value named "NPC" and have it search for that.

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