Problem With A following Npc

  1. What do you want to achieve?

well, I want to make my Npc follow a player and my script is not doing anything

  1. What is the issue? Include screenshots / videos if possible!

well when i test the game the npc won’t move at all! NO ERRORS IN THE OUTPUT

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I tried To Change My Script But It Didn’t worked, And Yes I Searched The Developer Hub And Didn’t Find My Answer!

I Appreciate Anything that could help me to fix this problem!

Here is The Script:

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]
		if (temp2.className == "Model") and (temp2 ~= script.Parent) then
			temp = temp2:FindFirstChild("Torso") or temp2:FindFirstChild("UpperTorso")
			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
--wait(math.random(0,5)/10)
while true do
	wait(0.5)
	local target = findNearestTorso(script.Parent.Torso.Position)
	if target ~= nil then
		script.Parent.Humanoid:MoveTo(target.Position, target)
	end
	end
1 Like

how is the script supposed to work?, What should he do
You have “findNearestTorso” but what pos ?

1 Like

You used children() which isn’t a function, you probably meant GetChildren()

1 Like

@Med367367 He Is Going To Follow The Nearest Player Around

@Tamzy3D Thanks But It’s still not working :frowning:

Honestly, this code seems very impractical and not very stable.

I recommend using Roblox’s PathfindingService to give more accurate results and progress. There are lots of tutorial videos on how to do this, and it’s a way more steady method than what your code is doing.

1 Like

Thanks I’ll try that, I don’t even know why this isn’t working, i’m not getting any errors :frowning:

Does It Matter if I Use A LocalScript Or Script For This PathFinding?

I’m not an expert on PathfindingService, but from my experience, LocalScripts can only work they are within the Client (in other words, the character). Or if you’re working with Tweening or other Methods within the StarterGui. Basically, anything involving the Client in anyway.

So you’re question, it’s dependent on where you’re placing the script. If you’re placing it in the ServerScriptService, then I recommend just a normal Script.

1 Like

Thank You Your Method Just Fixed My Problem and it’s awesome!

1 Like