AI doesn't get around walls and instead walks into them (unsolved)

Hello! so i have a NPC that just chases the player, but when the player spawns, instead of walking down the stairs he just gets to the railing and stops moving due to the invisible barrier, and even when walking down the stairs if the player is next to the he’ll walk into the stairs’ barrier. this also happens when he needs to go around crates. I have tried modifiers but that didn’t work. also, i have created parts to see where is wants to go (waypoints) but none of them are even near the stairs. It seems to be related to the for loop that makes him walk to ever waypoint, because by setting the waypoint index to 2, he seems to be walking around, but the issue is he will then get stuck on benches when climbing them, and he doesn’t get stuck with the for loop. the game is singleplayer so i don’t have to worry about multiple players
image

script.Parent.Animate.Enabled = false
local anim = script.Parent.Humanoid:LoadAnimation(script.Parent.Stun)
anim:Play()
task.wait(30)
anim:Stop()
script.Parent.Animate.Enabled = true
game.ReplicatedStorage.RemoteEvents.Awaken:FireAllClients()
local pfs = game:GetService("PathfindingService")
local pathparams = {
	["AgentHeight"] = 7.5,
	["AgentRadius"] = 2.147,
	["AgentCanJump"] = true,
	["AgentCanClimb"] = true,
	["WaypointSpacing"] = math.huge,
	Costs = {
		["Stairstart"] = 1,
		["Stairend"] = 1
	}
}
local waypoints = {}
local torso = script.Parent.Torso
local humanoid = script.Parent.Humanoid
local root = script.Parent.HumanoidRootPart
local plrhumanoid = game.Players:GetPlayers()[1].Character.HumanoidRootPart
script.Parent.PrimaryPart:SetNetworkOwner(nil)
game.ReplicatedStorage.RemoteEvents.Death:FireAllClients(root, humanoid, plrhumanoid)
local path
local function Walk()
	path = pfs:CreatePath(pathparams)
	path:ComputeAsync(torso.Position, plrhumanoid.Position)
	local waypoints = path:GetWaypoints()
	local success, result = pcall(function()
		for _, nextwaypoint in pairs(waypoints) do
			local p = Instance.new("Part")
			p.Anchored = true
			p.Position = nextwaypoint.Position
			p.Size = Vector3.new(1, 1, 1)
			p.CanCollide = false
			p.Parent = workspace.LoadedMaps
			humanoid:MoveTo(nextwaypoint.Position)
		end
	end)
	if not success then
		warn(result)
	end
end
task.spawn(function()
	while task.wait() do
		Walk()
	end
end)

(there is no function in case the path is blocked because it constantly recomputes the path due to it chasing the player)

4 Likes

Lets go ahead and debug this.

  1. Is there any parts in LoadedMaps?
  2. Can you try select all the stairs and tag them StairPart, then add to Costs.

For Costs, they should look like so:

Costs = {
		Stairstart = 1,
		Stairend = 1
      }

Try that and let me know!

3 Likes

yeah, otherwise i wouldn’t be able to play
I have debugged a little more and i’ve noticed that if i don’t use a for loop and instead use a specific index number (waypoints[2].Position) it actually goes to the stairs but it gets stopped due to climbing on benches (which it can climb, and the for loops fixes that issue)

1 Like

I think I know how to fix your issue because I’m a pathfinding boy hold on.

1 Like

gotta love pathfinding ygm (when it works)

2 Likes
local function Walk()
	path = pfs:CreatePath(pathparams)
	path:ComputeAsync(torso.Position, plrhumanoid.Position)
	local waypoints = path:GetWaypoints()
	local success, result = pcall(function()
		for _, nextwaypoint in pairs(waypoints) do
			local p = Instance.new("Part")
			p.Anchored = true
			p.Position = nextwaypoint.Position
			p.Size = Vector3.new(1, 1, 1)
			p.CanCollide = false
			p.Parent = workspace.LoadedMaps
			humanoid:MoveTo(nextwaypoint.Position)
		end
	end)
	if not success then
		warn(result)
	end
end

I think the for loop is your issue kind of yo issue boy, you can try utilizing break in there when yo need to and also use some if statements IN THE for loop because what can happen is the NPC will just keep going in the path no matter what because it’s stuck in that loop.
so like this for example:

local function Walk()
	path = pfs:CreatePath(pathparams)
	path:ComputeAsync(torso.Position, plrhumanoid.Position)
	local waypoints = path:GetWaypoints()
	local success, result = pcall(function()
		for _, nextwaypoint in pairs(waypoints) do
if targetfound then
NPC:MoveTo(target)
-- just an example dont take it for real but i think you know how to implement this in yo system
end
			local p = Instance.new("Part")
			p.Anchored = true
			p.Position = nextwaypoint.Position
			p.Size = Vector3.new(1, 1, 1)
			p.CanCollide = false
			p.Parent = workspace.LoadedMaps
			humanoid:MoveTo(nextwaypoint.Position)
		end
	end)
	if not success then
		warn(result)
	end
end

Another possible issue that could be happening is the little task.spawn thing boi, that’s not good for you not good nununu. I tried using task.spawn for waypoints pretty bad son it’s a mistake ESPECIALLY WITH A WHILE LOOP.

task.spawn(function()
	while task.wait() do
		Walk()
	end
end)
2 Likes

the thing is, the game is singleplayer and it always knows where you are, so a target found statement wouldn’t be needed. there is def smth wrong with the for loop since going to the 2nd waypoint seems to make it go around but the issue is if i use that then if it gets on top of benches (which it can with the for loop) it stops moving

1 Like

can u attempt my debugs then reply

2 Likes

by tagging, you just mean the tag feature? i am not really sure how to use it lol, and again, the for loop is a bigger issue since it’s not just the stairs, it is any wall it seems.

1 Like

It is in properties, you must tag the parts for the costs to work.
image

2 Likes

Do i just tag them with StairPart?? sorry for being stupid lol. the stairs also have wedges on them so the player and bot can climb them

1 Like

Correct then add to Costs
Costs = { StairPart = 2 }

1 Like

it doesn’t work :frowning: also again,

it will walk into walls too if the player is on the other side instead of walking to the doorway (that is if the doorway is not close)

1 Like

Use the Simple Path module. I use it for all of my NPC pathfinding.

1 Like

Have you tried setting both of the costs to math.huge? You can do that to ensure that the NPC will never go there.

this thread is older, i fixed this by adding a MoveToFinished, however that will make it go to the old positions of the player. here’s the newer thread: AI goes to old waypoints

Dang, very silly mistake haha. I thought people knew this.

1 Like

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