The issue is that the NPC keeps jumping according to waypoints without stopping, even when it should. It gets stuck on ledges or obstacles and keeps jumping indefinitely. I attempted to fix this by implementing a cooldown for jumping and allowing the regular code to proceed during that time, but the problem still persists.
CODE:
local pathfindingService = game:GetService("PathfindingService")
local Players = game:GetService("Players")
local character = script.Parent
local humanoid = character.Humanoid
local path = pathfindingService:CreatePath({
AgentHeight = 6;
AgentRadius = 3;
AgentCanJump = true;
})
local runService = game:GetService("RunService")
local waypoints = nil
local nextwaypointIndex = nil
local blockedConnection = nil
character.PrimaryPart:SetNetworkOwner(nil)
local function findTarget()
local max_distance = math.huge
local nearestTarget = nil
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 < max_distance then
nearestTarget = target
max_distance = distance
end
if distance < 5 then
target.Humanoid:TakeDamage(5)
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 blockedConnection >= nextwaypointIndex then
blockedConnection:Disconnect()
findTarget(destination)
end
end)
nextwaypointIndex = 2
humanoid:MoveTo(waypoints[nextwaypointIndex].Position)
for index, waypoint in pairs(waypoints) do
if waypoint.Action == Enum.PathWaypointAction.Jump then
humanoid.Jump = true
if humanoid.Jump then
humanoid:MoveTo(waypoints[nextwaypointIndex].Position)
end
end
end
else
warn("no path", errorMessage)
end
end
while wait() do
local target = findTarget()
if target then
print(target.Name)
followPath(target.HumanoidRootPart.Position)
end
end