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?

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