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

get an npc to follow a specific player?

How would i make an npc follow a specific player only (like an owner). I found this script but it gets the npc to follow everyone. (my last question got taken down for being “too broad”, but i dont know what to do or how to get an answer, ive searched everywhere) [ oh and i already asked this question but i didnt get an answer ]

script:

local larm = script.Parent:FindFirstChild("HumanoidRootPart")

local rarm = script.Parent:FindFirstChild("HumanoidRootPart")

  

function findNearestTorso(pos)

local list = game.Workspace:children()

local torso = nil

local dist = 100

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 true do

wait(math.random(1,5))

local target = findNearestTorso(script.Parent.HumanoidRootPart.Position)

if target ~= nil then

script.Parent.Humanoid:MoveTo(target.Position, target)

end

  

end

2 answers

✓ Accepted answer

First of all, that's a free model, and it's pretty laggy, but if you're ok with that, sure, let me explain:

the 2 variables you have set "larm" and rarm" are pretty the same, and you don't need them.

What that script does is looping through all the players position EVERY TIME. and when it's close of it, it follows.

If you want it to go to a specific player, do this in the line 25:

if temp2.Name == "YOURNAME HERE" then

and that's all, also i again don't recommend using that, it's really old, laggy, and messy. So do whatever you want. Hope I helped.

3 comments

0

thanks ^ but i have two questions: 1) is there any Npc following scripts you would recommend?, and with the specific player following bit 2) how would i get the npc to stop behind the player?

0

hm... free modeled, no. I don't know any good free modeled follow scripts instead you do your own one, and about the Npc stop behin the player change the variable called "dist" on line 13 to a lower number

0

i have literally no idea how to make my own one, but thank you anyways

Insert a Script in ServerScriptService and add this inside it

game.Players.PlayerAdded:Connect(function(plr) -- player added
if plr.Name == "PLAYERNAME" then -- Put the player name in between the " "
repeat wait() until workspace:FindFirstChild(plr.Name) -- waits for character to load
local NPC = game:GetService('ServerStorage'):WaitForChild('NPC'):Clone() -- clones npc
NPC.Parent = workspace -- parents it to workspace
while wait() do -- loop
NPC:WaitForChild('Humanoid'):MoveTo(plr.Character:WaitForChild('UpperTorso').Position + Vector3.new(-3,0,4)) 
end
end
end)

Then put your NPC Into ServerStorage make sure its called "NPC" and it should work

(If you are using R6 change "UpperTorso" to "Torso")

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