Here’s the main issue of this script, the AI is supposed to navigate its way over car rubble and hills to the player, but very often, it gets to a point and continues jumping endlessly and doesn’t move forward.
I’ve tried moving the waypoint action and even debounce on the jumping itself, and while the debounce was active to continue normally pathfinding but it didnt work
heres what the code looked like:
--imagine code above this
if waypoint.Action == Enum.PathWaypointAction.Jump then
if debounce then findPath(destination)
else
humanoid.Jump == true
wait(2)
debounce == true
end
end
And heres the actual code as it is currently:
local pathfindingService = game:GetService("PathfindingService")
local Players = game:GetService("Players")
local character = script.Parent
local humanoid = character.Humanoid
local path = pathfindingService:CreatePath({
AgentHeight = 5;
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
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