NPC keeps jumping

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 
4 Likes
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

It seems here that you’re only telling the humanoid to move to the next position IF it can jump? Correct me if I’m wrong

3 Likes

well sort of, but its more so getting the individual waypoint, but i realize now that that might get in the way of the “jump” section of the script

1 Like

same thing’s happening again, i removed it but the original problem persists

Could you maybe send a video of your problem?

Try this:

for index, waypoint in pairs(waypoints) do 
	if waypoint.Action == Enum.PathWaypointAction.Jump then
		humanoid.Jump = true 
		humanoid:MoveTo(waypoints[nextwaypointIndex].Position)
	end
end

(ignore the fact that I replied 6 hours late)

Added it, same problem, sorry, might be annoying to continue hearing it.

1 Like