Hello, I’m having some issues making an NPC with a pathfinding system that chases you. I have no clue why the path is nil.
So, I made the script to warn about any computing errors, but the thing isn’t very helpful because the only warning it gives is “Failed to compute path: nil”, even though I didn’t find any issues in the pathfinding script.
I’ve tried looking at the documentation (not very useful) and researching the forums, but nothing relevant to my issue appears.
Even though, before I aded the error handling part, the npc ran at ridiculous speeds in big circles. (I honestly don’t know how since its speed is supposed to be 16)
If anyone knows what could be wrong, please tell me. Thanks!
↓ The entire script located in serverscriptservice, with explanations ans stuff. ↓
-- Create a folder to hold the NPCs
local npcFolder = Instance.new("Folder")
npcFolder.Name = "NPCs"
npcFolder.Parent = game.Workspace
-- Define the NPC template
local npcTemplate = game.ReplicatedStorage.duk
-- Function to find the closest player
local function findClosestPlayer(npc)
local minDistance = math.huge
local closestPlayer = nil
for _, player in ipairs(game.Players:GetPlayers()) do
local distance = (player.Character.PrimaryPart.Position - npc.PrimaryPart.Position).Magnitude
if distance < minDistance then
minDistance = distance
closestPlayer = player
end
end
return closestPlayer
end
-- Function to make an NPC chase the closest player
local function chaseClosestPlayer(npc)
local closestPlayer = findClosestPlayer(npc)
if closestPlayer then
local character = closestPlayer.Character
if character then
local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
if humanoidRootPart then
local path = game:GetService("PathfindingService"):CreatePath()
-- Attempt to compute the path, handle any errors
local success, pathResult = pcall(function()
return path:ComputeAsync(npc.PrimaryPart.Position, humanoidRootPart.Position)
end)
if success and pathResult then
if pathResult.Status == Enum.PathStatus.Success then
local waypoints = pathResult:GetWaypoints()
for i, waypoint in ipairs(waypoints) do
local direction = (waypoint.Position - npc.PrimaryPart.Position).unit
npc.Humanoid:MoveTo(npc.PrimaryPart.Position + direction * 3) -- Move towards the waypoint with an offset
npc.Humanoid.MoveToFinished:Wait()
if i == #waypoints then
break -- Stop moving if reached the last waypoint
end
end
else
warn("Failed to compute path:", pathResult.Status)
-- Handle pathfinding failure (e.g., unreachable destination) here
end
else
warn("Failed to compute path:", pathResult)
-- Handle pathfinding failure (e.g., unexpected error) here
end
end
end
end
end
-- Function to spawn an NPC
local function spawnNPC()
local npc = npcTemplate:Clone()
npc.Parent = npcFolder
local primaryPart = npc.PrimaryPart
if not primaryPart then
primaryPart = npc:WaitForChild("HumanoidRootPart")
end
primaryPart.Position = Vector3.new(0, 0, 0) -- Set initial position
while npc.Parent do
chaseClosestPlayer(npc)
wait(1) -- Wait for a while before updating the path
end
end
-- Function to handle player added event
local function onPlayerAdded(player)
player.CharacterAdded:Connect(function(character)
wait(1) -- Wait a moment for the character to fully load
spawnNPC()
end)
end
-- Spawn NPCs for existing players
for _, player in ipairs(game.Players:GetPlayers()) do
onPlayerAdded(player)
end
-- Handle player added event
game.Players.PlayerAdded:Connect(onPlayerAdded)