Hello, I am trying to create an NPC that chases my character but it must go through a big maze first, and it seems to not attempt to pathfind (without any errors) from a long range:
Bottom left is NPC, top right is my spawn.
The NPC moves only from a certain distance OR even if I put him on top, which makes me thing that theres too much in between and it doesn’t compute…
Can’t have a long gif so he does not try to find his path when hes inside the maze, but on top he seems to attempt to find it (but falls in and it stops again unless the path is simpler to the player)
The dummy code:
local PathfindingService = game:GetService("PathfindingService")
local Players = game:GetService("Players")
local function setNetworkOwnerOfModel(model, networkOwner)
for _, descendant in pairs(model:GetDescendants()) do
if descendant:IsA("BasePart") then
if descendant:CanSetNetworkOwnership() then
descendant:SetNetworkOwner(networkOwner)
end
end
end
end
local path = PathfindingService:CreatePath({
["AgentRadius"] = 1,
["AgentHeight"] = 5,
["AgentCanJump"] = true,
["WaypointSpacing"] = 3,
["Costs"] = {
["Water"] = 50
}
})
function checkw(t)
local ci = 3
if ci > #t then
ci = 3
end
if t[ci] == nil and ci < #t then
repeat ci = ci + 1 wait() until t[ci] ~= nil
return Vector3.new(1,0,0) + t[ci]
else
ci = 3
return t[ci]
end
end
function findPlayer()
for i, plr in pairs(Players:GetPlayers()) do
if plr.Character then
local chr = plr.Character
local hrp = chr.HumanoidRootPart
return hrp.CFrame.Position
end
end
end
local model = script.Parent
setNetworkOwnerOfModel(model, nil)
function pathFind(position)
local connection;
path = PathfindingService:FindPathAsync(model.PrimaryPart.Position, position)
local waypoint = path:GetWaypoints()
if path and waypoint or checkw(waypoint) then
if checkw(waypoint) ~= nil and checkw(waypoint).Action == Enum.PathWaypointAction.Walk then
model.Humanoid:MoveTo( checkw(waypoint).Position )
model.Humanoid.Jump = false
end
if checkw(waypoint) ~= nil and checkw(waypoint).Action == Enum.PathWaypointAction.Jump then
connection = model.Humanoid.Changed:connect(function()
model.Humanoid.Jump = true
end)
model.Humanoid:MoveTo( waypoint[3].Position )
else
model.Humanoid.Jump = false
end
if connection then
connection:Disconnect()
end
else
for i = 2, #waypoint do
model.Humanoid:MoveTo( waypoint[i].Position )
end
end
end
while wait (.1) do
local targetPosition = findPlayer()
print(targetPosition)
if targetPosition then
pathFind(targetPosition)
end
end
Appreciate anyone who helps me bust this case because it’s kind of annoying