Npc Getting stuck on walls

My Npc is getting stuck on walls only the torso and hrp are set to can collide I’m pretty new to making npcs and used a tutorial.
here’s my code:

local PathfindingService = game:GetService("PathfindingService")

local npc = script.Parent
local humanoid = npc:WaitForChild("Humanoid")
local hrp = npc:WaitForChild("HumanoidRootPart")
local typ = "Smile"
hrp:SetNetworkOwner(nil)

local walkAnim = humanoid.Animator:LoadAnimation(script.Walk)
local attackAnim = humanoid.Animator:LoadAnimation(script.Attack)

local pathParams = {
	AgentHeight = 2.5,
	AgentRadius = 4,
	AgentCanJump = false,
	AgentCanClimb = true,
}

local rayParams = RaycastParams.new()
rayParams.FilterType = Enum.RaycastFilterType.Exclude
rayParams.FilterDescendantsInstances = {npc}

local lastPos
local animPlaying = false

local RANGE = 60
local DAMAGE = 100


	
local function canSeeTarget(target)
	local orgin = hrp.Position
	local direction = (target.HumanoidRootPart.Position - hrp.Position).Unit * RANGE
	local ray = workspace:Raycast(orgin, direction, rayParams)
	
		if ray and ray.Instance then
		if ray.Instance:IsDescendantOf(target) then
		
			return true
		else
			return false
		end
	else
		return false
	end
end

local function findTarget()
	local players = game.Players:GetPlayers()
	local maxDistance = RANGE
	local nearestTarget
	
	for i, player in pairs(players) do
		if player.Character then
			local target = player.Character
			local distance = (hrp.Position - target.HumanoidRootPart.Position).Magnitude
			
			if distance < maxDistance and canSeeTarget(target) then
			
				nearestTarget = target
				maxDistance = distance
			end
		end
	end
	
	return nearestTarget
end

local function getPath(destination)
	local path = PathfindingService:CreatePath(pathParams)
	
	path:ComputeAsync(hrp.Position, destination.Position)
	
	return path	
end

local function attack(target)
	local distance = (hrp.Position - target.HumanoidRootPart.Position).Magnitude
	local debounce = false
	
	if distance > 5 then
		humanoid:MoveTo(target.HumanoidRootPart.Position)
	elseif distance < 3 then
		if debounce == false then
			debounce = true
			print(distance)
			local plr = game.Players:FindFirstChild(target.Name)
			--npc.Head.AttackSound:Play()
			game.ReplicatedStorage.Died:FireClient(plr,typ)
			walkAnim:Stop()
			humanoid.WalkSpeed = 0
			attackAnim:Play()
			script.Parent.Humanoid["Scare Stings 11"]:Play()
			wait(0.7)
			target.Humanoid.Health -= DAMAGE
			task.wait(0.5)
			debounce = false
			humanoid.WalkSpeed = 13 + workspace.Map.Difficulty.Value
			walkAnim:Play()
		end
	end
end

local function walkTo(destination)
	local path = getPath(destination)
	
	if path.Status == Enum.PathStatus.Success then
		for i, waypoint in pairs(path:GetWaypoints()) do
			path.Blocked:Connect(function()
				path:Destroy()
				--print("Path Blocked")
			end)
			
			if animPlaying == false then
				walkAnim:Play()
				animPlaying = true
			end
			
			attackAnim:Stop()
			
			local target = findTarget()

			if target and target.Humanoid.Health > 0 then
				lastPos = target.HumanoidRootPart.Position
				attack(target)
				--print("attacking")
				break
			else
				if waypoint.Action == Enum.PathWaypointAction.Jump then
					humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
				end
				
				if lastPos then
					humanoid:MoveTo(lastPos)
					humanoid.MoveToFinished:Wait()
					lastPos = nil
					--print("last pos")
					break
				else
					humanoid:MoveTo(waypoint.Position)
					humanoid.MoveToFinished:Wait()
				end
			end
		end
	else
		return
	end
end

local function patrol()
	local waypoints = workspace.Waypoints:GetChildren()
	local randomNum = math.random(1, #waypoints)
	walkTo(waypoints[randomNum])
end

while task.wait(0.2) do
	patrol()
end
1 Like

Can we see a video of your ai?

I assume the problem arises when the ai is chasing you?

1 Like

No, just when trying to get to waypoints. I am using run instead of play if that changes anything just a note when it teleports that’s me moving it

External Media

edit:sorry I didn’t think streamable would make you download it

1 Like

This is a complicated issue; I believe the problem MAY be occurring because of the agent radius is just too large. although I’m not completely sure. I suggest widening any doorways or lowering that agent radius a little bit.

Edit: i willt try to recreate your problem in studio.

1 Like

ok I’ll try that, like I said I’m not to familiar with NPCs, I thought agent radius had the the size of the model

1 Like

Ok that seemed to fix the door issue but now he can’t climb stairs which are one stud high.

1 Like

try increasing the hipheight of the model, that usually works

1 Like

he does seem to be floating a bit but I’ll mark this as the solution thanks.

1 Like

i am having a problem when the ai is chasing me the ai walks into walls. Do you know the solution?
Would really appreciate the help. If you know how to fix or why its happening let me know.

message me here or private message me.
if this thread closes then private message me. thanks again

1 Like

I sort of fixed the pathfinding, but i would like your point of view of what to do for the code so the npc does not walk into walls, for better and more efficient work. My code will stop walking for a sec every time will have to calculate path.

When I write an AI, I usually have an infinite loop that constantly moves the ai to the destination. So, when the destination changes, the AI changes paths. If you want your ai to not get stuck on walls, I suggest using SimplePath as it does all the pathfinding for you, and fixes ROBLOX’s Pathfinding bugs.

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