Hey guys agKing here I have a problem with NPC so basically the NPC is jumping way too soon and is struggle to come accross the obstacle to go where he has to go
Here’s the code of the PathFinding I’ve made
function CharacterHandler:MoveThrough(player, destinations)
local npc = script.Parent
local hum = npc:FindFirstChild("Humanoid")
local root = npc:FindFirstChild("HumanoidRootPart")
local PathfindingService = game:GetService("PathfindingService")
if not (hum and root) then
warn("⚠️ NPC invalide (humanoid ou root manquant)")
return
end
root:SetNetworkOwner(nil)
local function computePath(fromPos, toPos)
local path = PathfindingService:CreatePath({
AgentRadius = 6,
AgentHeight = 24,
AgentCanJump = true,
AgentJumpHeight = 8,
AgentMaxSlope = 45,
AgentMaxAcceleration = 1000,
WaypointSpacing = 4,
Costs = {
Stone = math.huge,
Rock = math.huge,
Mushroom = math.huge,
Blacksmith = math.huge,
Chest = math.huge
}
})
path:ComputeAsync(fromPos, toPos)
return path
end
for _, destination in ipairs(destinations) do
local targetPos = destination.Position
local path = computePath(root.Position, targetPos)
local currentWaypointIndex = 1
while npc and hum do
if not path or path.Status ~= Enum.PathStatus.Success then
print("⚠️ Path invalide → recalcul immédiat")
path = computePath(root.Position, targetPos)
continue
end
local waypoints = path:GetWaypoints()
if currentWaypointIndex > #waypoints then
print("♻️ Fin du path → recalcul")
path = computePath(root.Position, targetPos)
currentWaypointIndex = 1
continue
end
local waypoint = waypoints[currentWaypointIndex]
local dist = (root.Position - waypoint.Position).Magnitude
if waypoint.Action == Enum.PathWaypointAction.Jump then
hum:ChangeState(Enum.HumanoidStateType.Jumping)
hum.Jump = true
end
hum:MoveTo(waypoint.Position)
local recalcDelay = 2
local reached = hum.MoveToFinished:Wait(recalcDelay)
if not reached then
print("🔄 Recalcul proactif (NPC coincé ou lent)")
path = computePath(root.Position, targetPos)
currentWaypointIndex = 1
else
currentWaypointIndex += 1
end
if (targetPos - root.Position).Magnitude < 3 then
print("✅ Destination atteinte :", destination.Name or "inconnue")
break
end
end
end
npc.Parent = player:WaitForChild("PlotOwned").Value:WaitForChild("Adventurers")
AnimationHandler:StopAnimation("Walk")
AnimationHandler:PlayAnimation("Idle", true)
CharacterHandler:LookAtMonster()
end