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

Why isn't the NPC following me?

function findNearestTorso(pos)

	local dist = 2048

	local list = workspace:GetChildren()
	local torso = nil
	local temp = nil
	local human = nil
	local temp2 = nil

	if list ~= nil then
		for x = 1, #list do
		temp2 = list[x]

		if (temp2.className == "Model") and (temp2 ~= script.Parent) and (temp2:FindFirstChildOfClass("LocalScript")) then

			temp = temp2:findFirstChild("HumanoidRootPart")
			human = temp2:findFirstChildOfClass("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

end

while wait(1) do
	target = findNearestTorso(script.Parent.HumanoidRootPart.Position)
	x = math.random(1, 10)
	if x > 2 then
		if target ~= nil then
			script.Parent.Humanoid:MoveTo(target.Position)
		end
	else
		script.Parent:findFirstChildOfClass("Humanoid"):MoveTo(script.Parent.HumanoidRootPart.Position + Vector3.new(math.random(-10, 10), script.Parent.HumanoidRootPart.Position.Y, math.random(-10, 10)))
	end
end

This is a script i got from a friend that makes it so the enemy follows the player but for some reason it doesn't work how do i fix this?

3 comments

0

I assure you, you didn't get it from a friend lmao

0

no, his friend is the toolbox @greatneil80

0

i already figured out how to fix it

1 answer

ok i fixed it myself you are supposed to unanchor the humanoidrootpart which is set to true when you insert :/

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