Whenever a NPC opens a door it for some reason gets stuck in the doorway, what is the solution for this? Am I doing the pathfinding wrong here?
External MediaSometimes when it doesn’t get stuck it sort of just keeps hugging the door, I’ve put the PathfindingModifiers inside the doormodel and still nothing.
Code:
-- Server script
local runService = game:GetService('RunService')
local replicatedStorage = game:GetService('ReplicatedStorage')
local pathfindingService = game:GetService('PathfindingService')
local collectionService = game:GetService('CollectionService')
local modules = replicatedStorage:WaitForChild('modules')
local npc = script.Parent
local humanoid = npc:WaitForChild('Humanoid')
local player = workspace:WaitForChild('dogov_pain')
local playerRoot = player:WaitForChild('HumanoidRootPart')
local recalculate = false
npc.HumanoidRootPart:SetNetworkOwner(nil)
-- Create path with modifiers for doors
local npc_path = pathfindingService:CreatePath({
AgentRadius = 2.5,
AgentHeight = 4,
WaypointSpacing = 4,
AgentCanJump = true,
AgentCanClimb = true
})
local function chasePlayer()
if not npc:FindFirstChild("HumanoidRootPart") then return end
local distance = (npc.HumanoidRootPart.Position - playerRoot.Position).Magnitude
humanoid.WalkSpeed = 16
local success, errorMessage = pcall(function()
npc_path:ComputeAsync(npc.HumanoidRootPart.Position, playerRoot.Position)
end)
if success and npc_path.Status == Enum.PathStatus.Success then
local waypoints = npc_path:GetWaypoints()
if #waypoints > 1 then
humanoid:MoveTo(waypoints[2].Position)
-- Optional: Debug visualization
local part = Instance.new("Part")
part.Position = waypoints[2].Position
part.Size = Vector3.new(0.5, 0.5, 0.5)
part.Color = Color3.new(1, 0, 0)
part.Anchored = true
part.CanCollide = false
part.Parent = workspace
game:GetService("Debris"):AddItem(part, 0.1)
end
else
humanoid:MoveTo(playerRoot.Position)
end
end
chasePlayer()
runService.Heartbeat:Connect(function(deltaTime)
chasePlayer()
end)