I have this pathfinding code I followed a tutorial to create, It works really well.
But I want to add the ability for the NPC the script is used on to be able to jump, and I do not know how to add it.
-- 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
local function findTarget()
local maxdistance = 500
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
while wait() do
local target = findTarget()
if target then
print(target.Name)
followpath(target.HumanoidRootPart.Position)
end
end
local npcRootPart = script.Parent.HumanoidRootPart
npcRootPart:SetNetworkOwner(nil)
If anyone knows how to do it, please tell me how.
