My AI is jumping almost all the time

I was trying to make my AI Piggy-like, it works really good and it’s smooth. Only issue I am dealing with is the “spam jump”. It makes NPC unable to move.

Video:

1 Like

Bumping this. (letter limit 2u863781)

Do the AI’s Humanoid properties have these options disabled?
image

nope, UseJumpPower is enabled and JumpPower is 50.

Can you send your script? It would help with debugging it,

I’m afraid I cannot do that. I will make a game to download if you want to see it.

And you cannot share the script behind the main pathfind because? Surely it’d look something along the lines of
get waypoints
for loop
if action == jump
jump
else
move
end

1 Like

no that’s not like it. I don’t know, I will check.

Try turning all the options off like how it is in the screenshot

Bot was decompiled from original game, it’s the same.

aiTest.rbxl (173.0 KB)
if anyone wants to try the AI itself, here it is.


I suggest trying the AI here.

Looks like there it jumps everytime it thinks its stuck

Yeah I know, it’s terrible… Really.

I did saw your video, but I still want it to jump.

Don’t really know what else to suggest other than changing block of code where instead of jumping it’ll just go through whatever its blocking and if that doesnt work then teleport the npc somewhere else in the map if the function returns nil more than 10x or something good luck on your game though!:

Yeaahhhh… that would never work, not in million years.

Thats a common pathfinding error, its very common, it happens because the path calculates while the AI is in air(when jumping) and to party fix that set CanJump to False

Add trusses around everything you want the monster to go over. Make it so only the monster can collide with the trusses. Make can climb true and set jump height to 0

Roblox is telling your ai to jump all the time with its pathfind request.
You can either disable jumping entirely (not ideal) or find a work around I.e go around your map and setup p-

Your entire AI script does not make sense to me, following the 3rd waypoint constantly gets you nowhere.

Instead you should try to ignore the 1st-2nd waypoint go from 3 and follow up to lets say 7-10 waypoint then find a new path to not keep the ai that far behind.

I have NOT adjusted your script to do this for you, what I have done is added a visual path and printing when its jumping.
Look into for loops.

local RunService = game:GetService("RunService")
local PathfindingService = game:GetService("PathfindingService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

local targetDistance = 50000000000
local closestTarget

local enemyModel = script.Parent
local enemyHumanoid = enemyModel:FindFirstChildOfClass("Humanoid")
local enemyRootPart = enemyModel:WaitForChild("HumanoidRootPart")
local Head = enemyModel:WaitForChild("Head")

local isWalking = false
local isJumping = false
local isChasing = true

local hit_timer = 0.7

local wanderX, wanderZ = 10, 10

local notwalking = 0
local oldposition = enemyRootPart.Position
local canWander = true

local currentlyWandering = 0
local destinedPath
local discontinuedPoints
local coordinatedWaypoints

enemyModel.State.Value = "Chasing"

enemyHumanoid.Running:Connect(function(speed)
	if speed > 0 then
		if isWalking == false then
			enemyRootPart.FootStep:Play()
			isWalking = true
		end
	elseif speed == 0 then
		enemyRootPart.FootStep:Stop()
		isWalking = false
	end
end)

local f = Instance.new("Folder", workspace)
function visualPath(waypoints)
	f:ClearAllChildren()
	for i, v in pairs(waypoints) do
		local bCol = (v.Action == Enum.PathWaypointAction.Jump and BrickColor.new("Deep blue") or BrickColor.new("Black"))
		local pt = Instance.new("Part", f)
		pt.Name = "waypoint" .. i
		pt.Anchored = true
		pt.CanCollide = false
		pt.Transparency = 0.9
		pt.Material = Enum.Material.Neon
		pt.BrickColor = bCol
		pt.Position = v.Position
		pt.Shape = Enum.PartType.Ball
	end
	
end

local function fetchPossibleTargets(preferredBP)
	local targetCharacters = workspace:GetChildren()
	local closestTarget = nil
	local closestDistance = targetDistance
	local closestHumanoid = nil
	local closestRootPart = nil

	for _, playerTarget in ipairs(targetCharacters) do
		if playerTarget:IsA("Model") and playerTarget ~= script.Parent then
			local targetHumanoid = playerTarget:FindFirstChildOfClass("Humanoid")
			local targetRootPart = playerTarget:FindFirstChild("HumanoidRootPart")

			if targetHumanoid and targetRootPart and targetHumanoid.Health > 0 then
				local targetParent = targetRootPart.Parent
				if not targetParent:FindFirstChild("Enemy") 
					and not targetParent:FindFirstChild("Helper") 
					and not targetParent:FindFirstChild("IsStunned")
					and not targetParent:FindFirstChild("SoldierNPC") 
					and enemyRootPart.Anchored == false then
					
					if targetRootPart then
						local distance = (targetRootPart.Position - preferredBP).Magnitude

						if distance < closestDistance then
							closestDistance = distance
							closestTarget = playerTarget
							closestHumanoid = targetHumanoid
							closestRootPart = targetRootPart
						end
					end
				end
			end
		end
	end
	return closestRootPart, closestTarget
end

task.spawn(function()
	while task.wait(1.6) do
		if isWalking == false  then
			enemyHumanoid:MoveTo(Vector3.new(enemyRootPart.Position.X + math.random(-wanderX, wanderX), 0, enemyRootPart.Position.Z + math.random(-wanderZ, wanderZ)))
		end
	end
end)

while RunService.Heartbeat:Wait() do
	if isChasing == true and enemyRootPart.Anchored == false then
		local MainTorso = fetchPossibleTargets(enemyRootPart.Position)

		if MainTorso ~= nil then
			currentlyWandering = 1

			local path = PathfindingService:CreatePath({
				AgentCanJump = true,
				Costs = {
					Water = 20
				},
				PathSettings = {
					SupportPartialPath = true
				}
			})
			
			destinedPath = PathfindingService:FindPathAsync(enemyRootPart.Position, MainTorso.Position)
			coordinatedWaypoints = destinedPath:GetWaypoints()
			discontinuedPoints = coordinatedWaypoints

			local function checkCurrentPath(t)
				local movementI = 3
				if movementI > #t then
					movementI = 3
				end
				if t[movementI] == nil and movementI < #t then
					repeat
						movementI = movementI + 1
						task.wait()
					until t[movementI] ~= nil
					return Vector3.new(1, 0, 0) + t[movementI]
				else
					movementI = 3
					return t[movementI]
				end
			end

			if destinedPath and coordinatedWaypoints or checkCurrentPath(coordinatedWaypoints) then
				visualPath(coordinatedWaypoints)
				if checkCurrentPath(coordinatedWaypoints) ~= nil and checkCurrentPath(coordinatedWaypoints).Action == Enum.PathWaypointAction.Walk then
					enemyHumanoid:MoveTo(checkCurrentPath(coordinatedWaypoints).Position)
				end

				if checkCurrentPath(coordinatedWaypoints) ~= nil and checkCurrentPath(coordinatedWaypoints).Action == Enum.PathWaypointAction.Jump then
					task.spawn(function()
						if isJumping == false then
							isJumping = true
							enemyHumanoid.Jump = true
							print("jumping cus my point told me to")
							task.wait(1.6)
							isJumping = false
						end
					end)
					if checkCurrentPath(coordinatedWaypoints) then
						enemyHumanoid:MoveTo(checkCurrentPath(coordinatedWaypoints).Position)
					end
				end
			else
				for i = 3, #discontinuedPoints do
					enemyHumanoid:MoveTo(discontinuedPoints[i].Position)
				end
			end
		elseif MainTorso == nil then
			currentlyWandering = 0
			destinedPath = nil
			coordinatedWaypoints = nil
		end
	end
end