I have this code that I wrote, for some reason, the NPC won’t jump even though the “AgentCanJump” is set to true. I do not know why that is, and what I need to fix it.
I want to add jumping to jump over obstacles that the NPC may come across.
I tried looking at a tutorial that showed how to add jumping to pathfinders, but that didn’t work.
Tutorial link: Here
Script:
-- Variables --
local Pathfinding = game:GetService("PathfindingService")
local Players = game:GetService("Players")
local Runservice = game:GetService("RunService")
local path = Pathfinding:CreatePath({
AgentHeight = 6,
AgentRadius = 3,
AgentCanJump = true,
Costs = {
Water = 100;
DangerZone = math.huge
}
})
local Character = script.Parent
local humanoid = Character:WaitForChild("Zombanoid")
local waypoints
local nextWaypointIndex
local reachedConnection
local blockedConnection
-- Functions --
local function findTarget()
local maxdistance = math.huge
local nearestTarget
for index, player in pairs(Players:GetPlayers()) do
if player.Character then
local target = player.Character
local distance = (Character.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude
if distance < maxdistance and target.Humanoid.Health > 0 then
nearestTarget = target
maxdistance = distance
end
end
end
return nearestTarget
end
local function followpath(destination)
local success, errorMessage = pcall(function()
path:ComputeAsync(Character.PrimaryPart.Position, destination)
end)
if success and path.Status == Enum.PathStatus.Success then
waypoints = path:GetWaypoints()
blockedConnection = path.blocked:Connect(function(blockedWaypointIndex)
if blockedWaypointIndex >= nextWaypointIndex then
blockedConnection:Disconnect()
followpath(destination)
end
end)
if not reachedConnection then
reachedConnection = humanoid.MoveToFinished:Connect(function(reached)
if reached and nextWaypointIndex < #waypoints then
nextWaypointIndex += 1
humanoid:MoveTo(waypoints[nextWaypointIndex].Position)
else
reachedConnection:Disconnect()
blockedConnection:Disconnect()
end
end)
end
nextWaypointIndex = 2
humanoid:MoveTo(waypoints[nextWaypointIndex].Position)
else
warn("No good, Path not computed.", errorMessage)
end
end
-- Connections --
while wait() do
local target = findTarget()
if target then
print(target.Name)
followpath(target.HumanoidRootPart.Position)
end
end
-- Other --
local npcRootPart = script.Parent.HumanoidRootPart
npcRootPart:SetNetworkOwner(nil)
Please help me.