I am currently trying to make an NPC follow the player, but then stop when too close. Currently, it will run into the player, pushing them around.
Here’s the script. Please note that when then object “EuroHelmet” is mentioned, it is mentioned so the AI doesn’t follow the enemy NPC, but rather the player, who’s StarterCharacter has the “EuroHelmet” acessory.
function findNearestPlayer(Position)
wait(0.3)
local List = game.Workspace:children()
local Torso = nil
local Distance = 30
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) and (Temp2:findFirstChild("EuroHelmet") ~= nil) then
if (Temp.Position - Position).magnitude < Distance then
Torso = Temp
Distance = (Temp.Position - Position).magnitude
end
end
end
end
return Torso
end
while true do
local target = findNearestPlayer(script.Parent.HumanoidRootPart.Position)
if target ~= nil then
script.Parent.Humanoid:MoveTo(target.Position, target)
end
end
I’m relatively new to scripting and have not a clue how to make it do what I want, and cannot find any articles or videos about it. Help is appreciated!