Humanoid.Jump keeps looping

basically, I want to fix this humanoid.jump looping error, however, I must’ve tried a million times to figure out what to do. Nothing I have done has worked, at least not essentially, in the end, I truly have no idea what to do.

I tried using a separate index number… 2 or 3, still didn’t work. Here is the code:

local bot = script.Parent
local humanoid = bot:FindFirstChildWhichIsA(“Humanoid”)
local rootpart = bot:FindFirstChild(“HumanoidRootPart”)
local pfs = game:GetService(“PathfindingService”)

local targetDistance = 1000
local wanderX = 5
local wanderZ = 5
local currentlyWandering = false
local wanderingEnabled = true

local ambience = rootpart:FindFirstChild(“Ambience”)
if ambience then
ambience.Playing = true
end

local function FindNearestTarget()
local nearestTarget = nil
local nearestDistance = math.huge

for _, plr in pairs(game:GetService("Players"):GetPlayers()) do
	if plr.Character and plr.Character:FindFirstChild("HumanoidRootPart") and plr.Character:FindFirstChildWhichIsA("Humanoid") and plr.Character:FindFirstChildWhichIsA("Humanoid"):GetState() ~= Enum.HumanoidStateType.Dead then
		local target = plr.Character
		local distance = (rootpart.Position - target.HumanoidRootPart.Position).Magnitude

		if distance <= targetDistance and distance < nearestDistance then
			nearestTarget = target.HumanoidRootPart
			nearestDistance = distance
		end
	end
end

return nearestTarget

end

game:GetService(“RunService”).Heartbeat:Connect(function()
local currentTarget = FindNearestTarget()
local currentWaypoint = nil
local JumpWaypoint = nil

for _, v in pairs(script.Parent:GetDescendants()) do
	if v:IsA("BasePart") or v:IsA("MeshPart") then
		if not rootpart.Anchored then
			v:SetNetworkOwner(nil)
		end
	end
end

if currentTarget then
	local path = pfs:CreatePath({
		AgentHeight = 5,
		AgentRadius = 2,
		AgentCanJump = true
	})
	local success, errorMessage = pcall(function()
		path:ComputeAsync(rootpart.Position, currentTarget.Position)
	end)

	if success and path.Status == Enum.PathStatus.Success then
		local waypoints = path:GetWaypoints()
		currentWaypoint = waypoints[2]
		JumpWaypoint = waypoints[3]

		path.Blocked:Connect(function()
			path:ComputeAsync(rootpart.Position, currentTarget.Position)
		end)
	else
		warn("Path not computed:", errorMessage)
		return
	end
else
	currentlyWandering = true
end

if currentWaypoint then
	if currentWaypoint.Action == Enum.PathWaypointAction.Walk or currentWaypoint.Action == Enum.PathWaypointAction.Jump then
		currentlyWandering = false
	end
	if JumpWaypoint ~= nil and JumpWaypoint.Action == Enum.PathWaypointAction.Jump then
		humanoid.Jump = true
	end
	humanoid:MoveTo(currentWaypoint.Position)
elseif wanderingEnabled then
	currentlyWandering = true
end

end)

humanoid.Running:Connect(function(speed)
rootpart.FootStep.Playing = speed > 0
end)

1 Like

What are you trying to make? If the dummy runs it jumps?

2 Likes

it’s supposed to be a chase ai. but that keeps happening when i am on certain parts and the npc needs to jump it does that. stuck jumping.

it looks like its trying to jump off the platform, maybe add a wait? i dont know

how long should I set the jump to wait before doing so specifically?? I’ll see if this works.

maybe 3 seconds

chaarrrrrrrrrr

well, the npc waits to jump, and after the wait it does that same hopping thing again. Thanks though,

put it in a loop instead of a heartbeat.changed function, maybe your calling too many paths, .5 or 1 second(s) is good

I tried this and it did the same thing, kept hopping, it’s like on the edge of parts too, like when it hops onto whatever it’s trying to jump onto, it jumps onto it perfectly fine, but trying to jump off it does that hopping thing and rarely ever gets unstuck easily.

while true do
local currentTarget = FindNearestTarget()
local currentWaypoint = nil
for i, v in pairs(script.Parent:GetDescendants()) do
if v:IsA(“BasePart”) or v:IsA(“MeshPart”) then
if rootpart.Anchored == false then
v:SetNetworkOwner(nil)
end
end
end
if currentTarget ~= nil then
local function checkCurrentPath(waypoints, index)
if waypoints and waypoints[index] then
return waypoints[index]
else
return nil
end
end

	local destinedPath = pfs:CreatePath({
		AgentHeight = 5;
		AgentRadius = 2;
		AgentCanJump = true;
	})
	destinedPath:ComputeAsync(rootpart.Position, currentTarget.Position)
	local coordinatedWaypoints = destinedPath:GetWaypoints()
	local discontinuedPoints = coordinatedWaypoints

	local movementI = 3
	if checkCurrentPath(coordinatedWaypoints, movementI) then
		currentWaypoint = checkCurrentPath(coordinatedWaypoints, movementI)
		if currentWaypoint.Action == Enum.PathWaypointAction.Walk then
			humanoid.Jump = false
			currentlywandering = false
		elseif currentWaypoint.Action == Enum.PathWaypointAction.Jump then
			currentlywandering = false
			wait(.5)
			humanoid.Jump = true
		end
	else
		for i = 3, #discontinuedPoints do
			currentWaypoint = nil
			humanoid:MoveTo(discontinuedPoints[i].Position)
		end
	end
	destinedPath.Blocked:Connect(function()
		destinedPath:ComputeAsync(rootpart.Position, currentTarget.Position)
	end)
end
if currentWaypoint ~= nil then
	humanoid:MoveTo(currentWaypoint.Position)
else
	currentlywandering = true
end
wait()

end

could you give me the rbxl file?

npc.rbxl (75.3 KB)

npc.rbxl (75.4 KB)
i think i fixed it, i added a jump amount that counts everytime the npc jumps, it it hits 2 jumps, it recalculates its path. change the max jump amount if you want.

thanks a lot actually, 2 seems to work just fine. Thanks man. If I find any issues later on feel free to contact me again about it.

no problem, im only worried about that jerking it does when it recalculates the path but it seems to work fine, so.

one thing i noticed though is the npc sometimes jumps even when it’s not on the part it was jumping off of, like a bit more than what it’s supposed to.

I think that’s because the wait time is 0 seconds, so it creates a path every 0 seconds

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.