My game development has been working smoothly until recently when the pathfinding monster for my horror game suddenly started walking through parts that I have the labels for the pathfinding modifier set to a cost of math.huge().
I can’t figure out what is happening, as I’ve tried the same script in a fresh test place, and the script worked perfectly. The NPC refused to walk into the part with the pathfinding modifier active. I even checked a previous version of my game (Which I can confirm used to work perfectly with the pathfinding system), which now also ignores pathfinding modifiers.
I thought that maybe the issue is that there are too many pathfinding modifiers because my game does have quite a bit of those to make sure it doesn’t get stuck, or maybe some game scripts causing problems within the pathfinding service, but then why would old versions of my game cease to function as intended now with these modifiers?
I can’t pinpoint exactly when this started happening, but I noticed it when one of the playtesters mentioned that the monster wasn’t reacting to the flare item, which is meant to scare the monster away by using a part with a modifier with the “FlareZone” label.
Here is a simplified script for my pathfinding service (I cut out all of the parts that link to specific things in my game so it can be used in a fresh test place):
local Players = game:GetService("Players")
local Pathfind = game:GetService("PathfindingService")
local main = script.Parent.Parent
local startPos = main.HumanoidRootPart.Position
local nearestPlayer
local nearestDistance
local nearestSneakingPlayer
local nearestSneakingDistance
local activeTask
local climbing = false
local path = Pathfind:CreatePath({
AgentCanJump = true,
AgentCanClimb = true,
AgentRadius = 2,
Costs = {
Climb = 1,
Water = 20,
FlareZone = math.huge,
StuckZone = 200
}
})
local maxDistance = 1500
function checkRaycast(character)
local raycastResult = nil
if character then
local rayOrigin = main.HumanoidRootPart.Position
local rayDirection = character.HumanoidRootPart.Position - rayOrigin
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {main, character}
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
raycastResult = workspace:Raycast(rayOrigin, rayDirection, raycastParams)
end
return raycastResult
end
function update()
main.HumanoidRootPart:SetNetworkOwner(nil)
local nearestPlayer = nil
local nearestDistance = nil
local nearestSneakingPlayer = nil
local nearestSneakingPlayer = nil
for _, player in pairs(Players:GetPlayers()) do
if player.Character and player.Character:FindFirstChild("Humanoid") and player.Character.Humanoid.Health > 0 then
local character = player.Character
local distance = (character.HumanoidRootPart.Position - main.HumanoidRootPart.Position).Magnitude
if not character or distance > maxDistance or (nearestDistance and distance >= nearestDistance) then
continue
end
nearestDistance = distance
nearestPlayer = player
end
end
if nearestPlayer then
if checkRaycast(nearestPlayer.Character) ~= nil then
script.Parent.CanSeePlayer.Value = false
else
script.Parent.CanSeePlayer.Value = true
end
else
script.Parent.CanSeePlayer.Value = false
end
if script.Parent.WalkSpeed > 0 then
if nearestPlayer ~= nil then
script.Parent.TargetPlayer.Value = nearestPlayer.Character
else
script.Parent.TargetPlayer.Value = nil
end
if nearestDistance and nearestDistance > 65 then
script.Parent.WalkSpeed = 9
elseif nearestDistance and nearestDistance <= 65 then
script.Parent.WalkSpeed = 21
else
script.Parent.WalkSpeed = 9
end
if nearestPlayer and nearestPlayer.Character:FindFirstChild('HumanoidRootPart') then
path:ComputeAsync(main.HumanoidRootPart.Position, nearestPlayer.Character.HumanoidRootPart.Position)
else
path:ComputeAsync(main.HumanoidRootPart.Position, startPos)
end
local Waypoints = path:GetWaypoints()
table.remove(Waypoints, 1)
if activeTask ~= nil then
coroutine.close(activeTask)
end
activeTask = coroutine.running()
followPath(Waypoints)
end
end
function followPath(waypoints)
for _, point in ipairs(waypoints) do
main.Humanoid:MoveTo(point.Position)
local moveFinished = main.Humanoid.MoveToFinished:Wait()
if not moveFinished then
warn("Monster stuck; Skipping pathfind waypoint")
main.HumanoidRootPart.CFrame = CFrame.new(point.Position)
end
if point.Action == Enum.PathWaypointAction.Jump then
main.Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
end
end
end
while true do
task.wait(.25)
if script.Parent.TravelingToSeekLastSeen.Value == false and script.Parent:GetState() ~= Enum.HumanoidStateType.Climbing then
coroutine.wrap(function()
update()
end)()
end
end
I’ve tried with PassThrough both enabled and disabled.
I’m here to ask if anyone else has encountered this same issue, and if you can, please inform me of what you did to solve the problem. I’ve been at this for a while now and can’t figure out how to replicate the issue. I’m getting tired of trying to search for the issue myself and trying to find similar forums online.
If anyone would like further details on my situation, please ask! I’m not going to be by my computer for the next couple of days after posting this, so I might not be able to go into too much detail for a while. I might also have some very late responses during this time period.
Thank you so much for your time and help!
