Chase NPC Climbing Function Problem

Hey, i borrowed the chasing npc from somewhere with smart chasing. It haves precise jumping, accurate pathfinding, fast reaction time. Everything is alright but it lacks one thing, climbing. I tried editing it, but nope. I tried experimenting with pathfinding links but i still can’t do anything. I’am still a Lua beginner so i often ran to problems. I want to get help with making the pathfinding for climbing and coding the climbing to npc’s mind. Help will be appreciated. Script:

local SearchDistance = 1000000 	-- How far a player can be before it detects you
local aiDamage = 0
local canWander = false
local WanderX, WanderZ = 30, 30
function getHumanoid(model)
	for _, v in pairs(model:GetChildren()) do
		if v:IsA'Humanoid' then
			return v
		end
	end
end
local ai = script.Parent
local human = getHumanoid(ai)
local hroot = ai.HumanoidRootPart
local zspeed = hroot.Velocity.magnitude
local pfs = game:GetService("PathfindingService")
function GetPlayerNames()
	local players = game:GetService('Players'):GetChildren()
	local name = nil
	for _, v in pairs(players) do
		if v:IsA'Player' then
			name = tostring(v.Name)
		end
	end
	return name
end
spawn(function()
	while wait() do
		wait(0)
	end
end)
function GetPlayersBodyParts(t)
	local torso = t
	if torso then
		local figure = torso.Parent
		for _, v in pairs(figure:GetChildren()) do
			if v:IsA'Part' then
				return v.Name
			end
		end
	else
		return "HumanoidRootPart"
	end
end
function GetTorso(part)
	local chars = game.Workspace:GetChildren()
	local torso = nil
	for _, v in pairs(chars) do
		if v:IsA'Model' and v ~= script.Parent and v.Name == GetPlayerNames() then
			local charRoot = v:FindFirstChild'HumanoidRootPart'
			if (charRoot.Position - part).magnitude < SearchDistance then
				torso = charRoot
			end
		end
	end
	return torso
end
for _, zambieparts in pairs(ai:GetChildren()) do
	if zambieparts:IsA'Part' then
		zambieparts.Touched:connect(function(p)
			if p.Parent.Name == GetPlayerNames() and p.Parent.Name ~= ai.Name then -- damage
				local enemy = p.Parent
				local enemyhuman = getHumanoid(enemy)
				enemyhuman:TakeDamage(aiDamage)
			end
		end)
	end
end
-- no touchy
local path
local waypoint
local oldpoints
local isWandering = 0
if canWander then
	spawn(function()
		while isWandering == 0 do
			isWandering = 1
			local desgx, desgz = hroot.Position.x + math.random(-WanderX, WanderX), hroot.Position.z + math.random(-WanderZ, WanderZ)
			human:MoveTo( Vector3.new(desgx, 0, desgz) )
			wait(math.random(4, 6))
			isWandering = 0
		end
	end)
end
while wait() do
	local enemytorso = GetTorso(hroot.Position)	
	if enemytorso ~= nil then -- if player detected
		isWandering = 1
		local function checkw(t)
			local ci = 3
			if ci > #t then
				ci = 3
			end
			if t[ci] == nil and ci < #t then
				repeat
					ci = ci + 1
					wait()
				until t[ci] ~= nil
				return Vector3.new(1, 0, 0) + t[ci]
			else
				ci = 3
				return t[ci]
			end
		end
		
		path = pfs:FindPathAsync(hroot.Position, enemytorso.Position)
		waypoint = path:GetWaypoints()
		oldpoints = waypoint
		local connection;
		
		local direct = Vector3.FromNormalId(Enum.NormalId.Front)
		local ncf = hroot.CFrame * CFrame.new(direct)
		direct = ncf.p.unit
		local rootr = Ray.new(hroot.Position, direct)
		local phit, ppos = game.Workspace:FindPartOnRay(rootr, hroot)
		
		if path and waypoint or checkw(waypoint) then
			if checkw(waypoint) ~= nil and checkw(waypoint).Action == Enum.PathWaypointAction.Walk then
				human:MoveTo( checkw(waypoint).Position )
				human.Jump = false
			end

			if checkw(waypoint) ~= nil and checkw(waypoint).Action == Enum.PathWaypointAction.Jump then
				human.Jump = true
				connection = human.Changed:connect(function()
					human.Jump = true
				end)
				human:MoveTo( checkw(waypoint).Position )
			else
				human.Jump = false
			end
			
			hroot.Touched:connect(function(p)
				local bodypartnames = GetPlayersBodyParts(enemytorso)
				if p:IsA'Part' and not p.Name == bodypartnames and phit and phit.Name ~= bodypartnames and phit:IsA'Part' and rootr:Distance(phit.Position) < 5 then
					connection = human.Changed:connect(function()
						human.Jump = true
					end)
				else
					human.Jump = false
				end
			end)
			
			if connection then
				connection:Disconnect()
			end
			
		else
			for i = 3, #oldpoints do
				human:MoveTo( oldpoints[i].Position )	
			end
		end
	elseif enemytorso == nil and canWander then -- if player not detected
		isWandering = 0
		path = nil
		waypoint = nil
		human.MoveToFinished:Wait()
	end
end

AIChase.lua (4.1 KB)

2 Likes

I remember using this script before, I made the npc climb by replacing
“path = pfs:FindPathAsync(hroot.Position, enemytorso.Position)”
with
“path = pfs:CreatePath(
{AgentRadius = 2,
AgentHeight = 3,
AgentCanJump = true,
AgentCanClimb = true,
WaypointSpacing = 5})
path:ComputeAsync(hroot.Position, enemytorso.Position)”

1 Like

Also, the script you are using has a bad detecting function, because it doesn’t go to the closest player, so maybe change it too.

1 Like

I copy-pasted the snippet you gave me and kinda worked. It increased the smoothness of the movement but sadly, it didn’t help with climbing. Since i’m overall Lua idiot, i really can’t be fully conscious what the hell is going on with the script but i tried to fix this with making pathfinding links because i seen that games with working npcs used it to make the npcs climb. so i added it and well the same effect. i tried to change some properties but no. i don’t know if i made a mistake with placing attachments or i named the label wrong (if that even matters). but we are closer beacuse the npc actually stop right in front of a ladder. in previous version it instantly stopped after no sign of any waypoints. i wonder if there is something needed to change in the script to support those links or my truss part is cursed.
Zrzut ekranu 2025-05-02 231000

1 Like

What are the properties of the pathfinding link, maybe that’s the problem?

The video has that character attempting to climb the ladder (brass tube character my goat), but that character has a different ai made by me, I don’t really know too much about pthfinding systems, but this ai works for some reason. (Don’t ask what’s happening in the video, idk either)

1 Like

here is the properties:


i honestly don’t know if placed the attachments correctly or it just dosen’t support the links.

Turn on bidirectional, also attachment 0 is the start of the link, though you probably already knew that.

nothing, the npc just stays next to the ladder. does the script even support pathfinding links?
image