Hi there
I have some problems that my script doesn’t work. Here is my script
--{{Service}}--
local PathFindingService = game:GetService("PathfindingService")
local Players = game:GetService("Players")
--{{Model}}--
local NPC = game.Workspace:FindFirstChild("NPC")
local NPCRootPart = NPC:FindFirstChild("HumanoidRootPart")
--{{Distance}}--
local Distance
local ClosestDistance = math.huge
local ClosestPlayer
--{{Loop}}--
while task.wait() do
--{{Find the closest player and closest distance}}--
for _,player in ipairs(Players:GetPlayers()) do
--{{character}}
local character = player.Character
local PrimaryPart = character:FindFirstChild("HumanoidRootPart")
--{{Distance Update}}--
Distance = (NPCRootPart.Position - PrimaryPart.Position).Magnitude
if Distance < ClosestDistance then
ClosestDistance = Distance
ClosestPlayer = character
end
end
--{{Path}}--
local path = PathFindingService:CreatePath()
--{{ComputeAsync}}--
local success, error = pcall(function()
path:ComputeAsync(NPCRootPart.Position, ClosestPlayer.PrimaryPart:GetPivot().Position)
end)
--{{Move to waypoints}}--
for _,waypoint in ipairs(path:GetWaypoints()) do
if success and path.Status == Enum.PathStatus.Success then
NPC.Humanoid:MoveTo(waypoint)
else
warn(error)
end
end
end
@Dammio_6 use this simple chase nearest player script Instead if none of what i said works
local NPC = game.Workspace:FindFirstChild("NPC")
local runservice = game:GetService("RunService")
local function Closest()
local range = math.huge
local target = nil
for _,player in pairs(game.Players:GetPlayers()) do
local character = player.Character or player.CharacterAdded:Wait()
if character then
local distance = (NPC.PrimaryPart.Position - character.PrimaryPart.Position).Magnitude
if distance < range then
range = distance
target = character
end
end
end
return target
end
runservice.Heartbeat:Connect(function()
local target = Closest()
if target then
NPC.Humanoid:MoveTo(target.PrimaryPart.Position)
end
end)