while task.wait(1) do
print("started")
local npc = script.Parent -- the path to the NPC
local human = npc.Humanoid -- getting the humanoid of the npc
local ServerStorage = game:GetService("ServerStorage")
local SimplePath = require(ServerStorage.SimplePath)
local Range = 1000
for i,v in ipairs(game.Players:GetChildren()) do
print("yes")
if (v.Character.HumanoidRootPart.Position - npc.Sphere.Position).magnitude <= Range then
print("got plr")
local path = SimplePath.new(npc)
path.Visualize = true
path:Run(v.Character.HumanoidRootPart.Position)
print("running")
path.Blocked:Connect(function()
path:Run(v.Character.HumanoidRootPart.Position)
end)
else
end
end
end
I took the liberty of re-writing the code for you:
if not (script.ClassName == "Script" and script.RunContext == Enum.RunContext.Legacy) then error("Script is most likely client-sided") end
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local RANGE = 1000
local SimplePath = require(ServerStorage.SimplePath)
local npc = script.Parent
local humanoid = npc.Humanoid
local connection
print("starting loop")
while true do
for _, player in Players:GetPlayers() do
if player.Character and (player.Character.PrimaryPart.Position - npc.Sphere.Position).Magnitude <= RANGE then
print("player detected")
local path = SimplePath.new(npc)
path.Visualize = true
path:Run(player.Character.PrimaryPart.Position)
print("running")
if connection then continue end -- To prevent rewriting the Blocked connection
connection = path.Blocked:Once(function()
path:Run(player.Character.PrimaryPart.Position)
connection = nil
end)
end
end
task.wait(1)
end
Hopefully it will work, and it should be more efficient than the current code
I’ve added a line of code to help with debugging, if need be