well, I want to make my Npc follow a player and my script is not doing anything
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
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
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.
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.