Roblox pathfinding is really weird


image
really weird issue im having right now, not sure what to do about it, is there something wrong with the code or do things need to be built in a specific way is what im kinda wondering right now.
here is the code for one of the enemies (specifically the tv guy which is seen in two of the image)

local Theef = script.Parent
local humanoid = Theef:WaitForChild("Humanoid")
local waypointsFolder = workspace:WaitForChild("enemyWaypoints")
local pfS = game:GetService("PathfindingService")

Theef:WaitForChild("HumanoidRootPart")

Theef.PrimaryPart:SetNetworkOwner(nil)

local function canSeeTarget(target)
	local origin = Theef.HumanoidRootPart.Position
	local direction = (target.HumanoidRootPart.Position - Theef.HumanoidRootPart.Position).Unit * 100

	local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {Theef}
	raycastParams.FilterType = Enum.RaycastFilterType.Exclude
	raycastParams.CollisionGroup = "InvisWalls"

	local raycastResult = workspace:Raycast(origin, direction, raycastParams)

	if raycastResult then
		if raycastResult.Instance:IsDescendantOf(target) then
			return true
		end
	end
	return false
end


local function findTarget()
	local players = game.Players:GetPlayers()
	local maxDist = 100
	local nearestTarget
	
	for index, player in pairs(players) do
		if player.Character then
			local target = player.Character
			local distance = (Theef.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude
			
			if distance < maxDist and (player.Backpack:FindFirstChild("Scrap") or player.Character:FindFirstChild("Scrap"))  and canSeeTarget(target) then
				nearestTarget = target
				maxDist = distance
			end
		end
	end
	
	return nearestTarget
end

local function getPath(destination)
	local pathParams = {
		AgentRadius = 2,
		AgentHeight = 10,
		AgentMaxSlope = 45,
		WaypointSpacing = 2
	}

	local path = pfS:CreatePath(pathParams)

	path:ComputeAsync(Theef.HumanoidRootPart.Position, destination.Position)

	return path
end

local function attack(target)
	local distance = (Theef.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude
	if distance > 2 then
		local path = getPath(target.HumanoidRootPart)
		humanoid:MoveTo(target.HumanoidRootPart.Position)
	else
		target.Humanoid.Health = 0
	end
end

local function walkTo(destination)
	local path = getPath(destination)
	
	if path.Status == Enum.PathStatus.Success then
		for index, waypoint in pairs(path:GetWaypoints()) do
			local target = findTarget()
			if target and target.Humanoid.Health > 0 then
				Theef.Humanoid.WalkSpeed = 22
				attack(target)
				break
			else
				Theef.Humanoid.WalkSpeed = 16
				humanoid:MoveTo(waypoint.Position)
				humanoid.MoveToFinished:Wait()
			end
		end
	else
		humanoid:MoveTo(destination.Position - (Theef.HumanoidRootPart.CFrame.LookVector * 10))
	end
end

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

while true do
	patrol()
end

ive researched a bit into this and the only solution i keep seeing is to make your own pathfinding system or there just isnt any info about it really (or there is also those modules that create better pathfinding scripts but im unsure of how those work lol)

in short, is there either an alternative to this or am i doing something wrong?

1 Like

You want to add in path parameters ( they may be called something else, haven’t done pathfinding in a while) and all they do is allow you list how big your model is, how tall it is and if it can jump or do other tasks

dont know why i forgot to ever reply but my code already has path params in it and they are used, inside getpath function you can see the params ive used, they dont help at all and messing around with the numbers barely gives different results. again sorry about like not replying for 6 months straight, would still hope to find a way to fix this issue…

Pathfinding works on a 2x2 voxel system. Try making walls at least two studs thick. Use debugging tools to visualize the paths so you can know what works in development.

1 Like

what if i want to still keep the thin walls in some areas? is there a possible workaround for that or am i just forced to work with the system and make walls thicker?

You’ll want to put thicker (2x2), invisible walls that don’t collide on all of your think walls. Then insert a pathfinding modifier object, and change it’s Label property to “dontgohere” (or whatever else you want).

When you’re setting pathParams, make a dictionary called “Costs”, make an entry named “dontgohere” and then set the value to math.huge. This example probably explains it better.

local pathParams = {
	AgentRadius = 2,
	AgentHeight = 10,
	AgentMaxSlope = 45,
	WaypointSpacing = 2,
	Costs = {
		dontgohere = math.huge;
	}
}