Trouble with Choosing the Correct Path


Basically I’m trying to code a Pathfind System for a NPC, but whenever it finds the player no matter what the path is generating it is trying to walk through any object (the image above is an example) heres the code im using

local Chase = false
local Ribbit = script.Parent
local RibbitHRP = Ribbit.HumanoidRootPart
local RibbitHumanoid = Ribbit.Humanoid

local PFS = game:GetService("PathfindingService")
local Players = game:GetService("Players")

local GlobalPath
local Points 
local Range = 50 --- amount of studs til the Animatronic can see

local ChaseSpeed = 20

local RunService = game:GetService("RunService")
local State = script.Parent.State



local ChaseAnim = script.TestAnim
local Animator : Animator = RibbitHumanoid.Animator

local ChaseTrack : AnimationTrack = Animator:LoadAnimation(ChaseAnim)
ChaseTrack.Looped = true

local AgentParams = {
	AgentRadius = 10,
	AgentHeight = 7.5,
	AgentCanJump = false,
	AgentCanClimb = false,
	WaypointSpacing = 2.5,

}


local function CreatePlayerPath(target : Player)
	local PlayerPath = PFS:CreatePath(AgentParams)
	local targetHRP = target.Character:WaitForChild("HumanoidRootPart")
	
	local success,ErrorMSG = pcall(function()
		PlayerPath:ComputeAsync(RibbitHRP.Position,targetHRP.Position)
	end)
	
	if not success or PlayerPath.Status ~= Enum.PathStatus.Success then
		return nil
	end
	
	return PlayerPath
end


local function GetClosestPlayer()
	local MagnitudesTable = {}
	local target = nil
	
	for _, player in pairs(Players:GetPlayers()) do
		local distance =  player:DistanceFromCharacter(RibbitHRP.Position)
		
		if distance <= Range then
			table.insert(MagnitudesTable,{distance, player})
		end
	end

	table.sort(MagnitudesTable,function(a,b)
		return a[1] < b[1]
	end)
	
	if #MagnitudesTable > 0 then
		target = MagnitudesTable[1][2]
	end
	
	warn("Target: ", target)
	return target
	
end

local function Patrol()
	local PatrolPoints = game.Workspace.PatrolPoints
	
	local RandomPoint = PatrolPoints:GetChildren()[math.random(1,#PatrolPoints:GetChildren())]
	

	
	local  WaypointPath = PFS:CreatePath(AgentParams)
	
	
	local success,ErrorMSG = pcall(function()
			WaypointPath:ComputeAsync(RibbitHRP.Position,RandomPoint.Position)
	end)
	
	if success then
		
		local Waypoints = WaypointPath:GetWaypoints()
			warn("FOUND A PATH")

			
		for _, Waypoint in pairs(Waypoints) do
			local Part = Instance.new("Part")
			Part.Parent = workspace
			Part.Anchored = true
			Part.Position = Waypoint.Position
			Part.CanCollide = false
			Part.Size = Vector3.new(1,1,1)
			
			RibbitHumanoid:MoveTo(Waypoint.Position)
			
			
			
			
			RibbitHumanoid.MoveToFinished:Wait()
			
		end
		
		
	end
	
end


local function ActivateChase()
	State.Value = "Chase"
	RibbitHumanoid.WalkSpeed = ChaseSpeed
	
end
 

 
while task.wait(.1) do
	local target : Player = GetClosestPlayer()
	
	if target  then
		repeat task	.wait() until target.Character or target.CharacterAdded:Wait() and target.Character:FindFirstChild("Humanoid").Health ~= 0
		GlobalPath = CreatePlayerPath(target)
	
	if GlobalPath then
			Points = GlobalPath:GetWaypoints()
		ActivateChase()	
			
		for _, WP in pairs(Points) do
			RibbitHumanoid:MoveTo(WP.Position)
			
			local Part = Instance.new("Part")
			Part.Parent = workspace
			Part.Anchored = true
			Part.Position = WP.Position
			Part.Material = "Neon"
			Part.CanCollide = false
			Part.Size = Vector3.new(1,1,1)
			
			
			game.Debris:AddItem(Part,.5)
			
			
			
			
		end
			
	end
	else
		Patrol()
	end
	RunService.Heartbeat:Wait()
	
end

Sorry it did not appear formatted for me at thes tart?? it was so messy it just ppeared properly now om

yea sorry bout that im not very good at these fourms

1 Like

Is this a custom rigged character?

yes it is a custom character, it has a roblox rig inside of it with all of its custom parts welded together (sorry if it didnt make sense)

For starters, make sure the wall it’s trying to navigate around is anchored, has collision, and is in Workspace/a child of Workspace.

Also, try setting the destination it walks to a little bit further away from the wall. Keep in mind i don’t really deal with pathfinding at all so this is just general advice.

2 Likes

User the Luaucodecheck in the text container is a Unluau and a ungoodthink towards Miniluau (Ministry of Luau) please do not speak bad duckspeakPlusCrimeluau.

  • Closure inside empty argument is a ungoodthinkPlusUnLuau and you must collapse it inside Goodluau pcall.
  • Luaucodecheck inside text container is not completed that doubleuncompletedplusbadthink
  • pairsplusipairs is a remnants of a oldspeak (badspeakPlusUnluau)
  • Vector3 is a oldspeakPlusUnluau and you must use Goodluau analog vector.create that is doubleplusgood perfomance
  • Toughtcrime against Luaucache variables outside of function scope is doubleplusungood
  • callback inside table.sort without Luaucache is a badthink
  • user you could be referancing wrong humanoid and that a crimethink against Miniluv

bro I saw this and thought this was a motivational developer discussion post not something about pathfinding systems

2 Likes