Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

My NPC follow script doesn't follow everybody, is there a reason?

Asked by 4 years ago
Edited 4 years ago

Hey everyone, so I've made this NPC and got this script working, but my friend (we do a Team Create thing) can't get it to follow him. We have a custom character so every player should be the same. Thanks in advance! (Code Below)




function findNearestTorso(pos) local list = game.Workspace:children() local torso = nil local dist = 45 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("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 wait() do wait(1) local target = findNearestTorso(script.Parent.HumanoidRootPart.Position) if target ~= nil then script.Parent.Humanoid:MoveTo(target.Position, target) end end

Answer this question