My pathfinding script will seemingly stop for no reason at random times. I have a line that prints the path status, and it prints “no path”. I initially thought it might have to do with the fact that agentCanJump was set to false, but im not sure about that anymore. This is my first actual pathfinding script, I found one online and modified it and added the monster’s vision stuff, so it may be a little messy or obvious what the problem was. I took out a lot of crap to make it easier to read.
local PathfindingService = game:GetService("PathfindingService")
local PlayerService = game:GetService("Players")
local TweenService = game:GetService("TweenService")
local viewRange = 75
local fieldOfView = 70
local npc = script.Parent
local humanoid = npc.Humanoid
local hrp = npc.HumanoidRootPart
hrp:SetNetworkOwner(nil)
local chasing = script.Parent.Chasing
local walkAnim:AnimationTrack = humanoid.Animator:LoadAnimation(script.Walk)
local attackAnim = humanoid.Animator:LoadAnimation(script.Attack)
local pathParams = {
AgentHeight = 13,
AgentRadius = 3,
AgentCanJump = false,
}
local rayParams = RaycastParams.new()
rayParams.FilterType = Enum.RaycastFilterType.Exclude
rayParams.FilterDescendantsInstances = {npc}
local lastPos
local animPlaying = false
local RANGE = 60
local DAMAGE = 30
local function getCharacters()
local characters = {}
local players = PlayerService:GetPlayers()
for index, player in players do
table.insert(characters, player.Character)
end
return characters
end
local function raycast(npc, start, finish)
local parameters = RaycastParams.new()
parameters.FilterDescendantsInstances = {npc, getCharacters()}
parameters.FilterType = Enum.RaycastFilterType.Exclude
local raycast = workspace:Raycast(start, (finish - start), parameters)
if raycast then
return true
else
return false
end
end
local function checkSight(npc)
local detectable = {}
local characters = getCharacters()
for index, character in characters do
table.insert(detectable, character)
end
for index, target in detectable do
if target:IsA("Model") then
target = target.PrimaryPart
end
--npc = script.Parent
local headPosition = npc["Cube.001"].LowerTorso.MidTorso.UpperTorso.Neck.Head.WorldPosition
local objectPosition = target.Position
local headCFrame = npc["Cube.001"].LowerTorso.MidTorso.UpperTorso.Neck.Head.WorldCFrame*CFrame.Angles(math.rad(-15),0,0)
local npcToObject = (objectPosition - headPosition).Unit
local npcLookVector = headCFrame.LookVector
local dotProduct = npcToObject:Dot(npcLookVector)
local angle = math.deg(math.acos(dotProduct))
local distance = (headPosition - objectPosition).Magnitude
if angle > fieldOfView then
return
end
if distance > viewRange then
return
end
if raycast(npc, headPosition, objectPosition) then
return
end
return target.Parent
end
end
local function findTarget()
local players = game.Players:GetPlayers()
local maxDistance = RANGE
local nearestTarget
local targetFound = false
local target = checkSight(npc)
if target then
if target:IsA("Model") then
nearestTarget = target
end
end
return nearestTarget
end
local function getPath(destination)
local path = PathfindingService:CreatePath(pathParams)
path:ComputeAsync(hrp.Position, destination.Position)
return path
end
local function attack(target)
local distance = (hrp.Position - target.HumanoidRootPart.Position).Magnitude
local debounce = false
chasing.Value = true
if distance > 10 then
local playedSound = false
humanoid.WalkSpeed = 25
humanoid:MoveTo(target.HumanoidRootPart.Position)
elseif distance < 10 then
if debounce == false then
--jumpscare crap
end
end
end
local function walkTo(destination)
local path = getPath(destination)
print(path.Status)
if path.Status == Enum.PathStatus.Success then
humanoid.WalkSpeed = script.Parent.NormalSpeed.Value
for i, waypoint in pairs(path:GetWaypoints()) do
humanoid.WalkSpeed = script.Parent.NormalSpeed.Value
path.Blocked:Connect(function()
path:Destroy()
end)
if animPlaying == false then
walkAnim:Play()
animPlaying = true
end
attackAnim:Stop()
local target = findTarget()
if target and target.Humanoid.Health > 0 and target.Values.Hiding.Value == false then
lastPos = target.HumanoidRootPart.Position
attack(target)
else
chasing.Value = false
if lastPos then
humanoid:MoveTo(lastPos)
humanoid.MoveToFinished:Wait()
lastPos = nil
break
else
humanoid:MoveTo(waypoint.Position)
humanoid.MoveToFinished:Wait()
end
end
end
else
return
end
end
local function patrol()
local waypoints = script.Parent.Parent.Waypoints:GetChildren()
local randomNum = math.random(1, #waypoints)
walkTo(waypoints[randomNum])
end
while task.wait(0.2) do
patrol()
end
Here’s an example:
why would it stop here??? Thanks for any help.
EDIT!! It seems to be because jumping is off. I turned it back on, and it worked, but the monster ended up jumping over shelves, and clipping through the roof, and trampling over objects. I believe the problem is that even though jump is off, it still tries to set the waypoint action to jump, but can jump is false, so it ends up stopping. What would I need to add/change in order to fix this, since having him stop or jump are both huge emersion breakers? Also sorry for any typos I didn’t notice, my arm is broken so im making a lot of typos