Hey! Tried to make an NPC follow me, and it works so far. However, after the NPC returns back to its default position, it doesn’t chase the player anymore. Perhaps I don’t set the target variable back to the player, here is my code. (All in a server script btw)
local c = script.Parent
local Humanoid = c.Humanoid
local Target = nil
local function check_range()
while true do
wait(1)
if Target ~= nil then
if (c.HumanoidRootPart.Position - game.Workspace.NPCSpawn.Position).Magnitude < 150 then
Target = nil
Humanoid:MoveTo(game.Workspace.NPCSpawn.Position)
end
end
end
end
while true do
wait(0.5)
if Target ~= nil then
print("found target")
Humanoid:MoveTo(Target.HumanoidRootPart.Position)
spawn(check_range)
end
if Target == nil then
for i, v in pairs(game.Workspace:GetChildren()) do
if v:IsA("Model") and v:FindFirstChild("HumanoidRootPart") and v ~= c then
if (c.HumanoidRootPart.Position - v.HumanoidRootPart.Position).Magnitude < 50 then
if (c.HumanoidRootPart.Position - v.HumanoidRootPart.Position):Dot( c.HumanoidRootPart.CFrame.lookVector ) < 0 then
print("NPC is now targetting ".. v.Name)
Target = v
end
end
end
end
end
end
Please help!