Pathfinding not working

This is my script, it says that it cannot cast onto object on line 16 i cannot find a solution yet.

local PathfindService = game:GetService("PathfindingService")

local bear = script.Parent
local hum = bear:FindFirstChild("Humanoid")
local hrp = bear:FindFirstChild("HumanoidRootPart")
hrp:SetNetworkOwner(nil)

local pathPrms = {
	AgentHeight = 5,
	AgentRadius = 2,
	AgentCanJump = false
}

local rayPrms = RaycastParams.new()
rayPrms.FilterType = Enum.RaycastFilterType.Exclude
rayPrms.FilterDescendantsInstances = (rayPrms)

local lastPos
local status

local RANGE = 40
local DMG = 100

local function seeTarget(target)
	local ORGN = hrp.Position
	local direction = (target.HumanoidRootPart.Position - hrp.Position).Unit * RANGE
	local ray = workspace:Raycast(ORGN, direction, rayPrms)
	
	
	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 MXdistance = RANGE
	local nearTarget
	
	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 < MXdistance and seeTarget(target) then
				nearTarget = target
				MXdistance = distance
			end
		end
	end
end

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


local function attackTarget(target)
	local distance = (hrp.Position - target.HumanoidRootPart.Position).Magnitude
	local db = false
	
	if distance > 5 then
		hum:MoveTo(target.HumanoidRootPart.Position)
	else
		if db == false then
			db = true
			
			bear.Attack:Play()
			target.Humanoid.Health -= DMG
			task.wait(0.5)
			db = false
		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()
			end)
			
			local target = findTarget()
			
			if target and target.Humanoid.Health > 0 then
				lastPos = target.HumanoidRootPart.Position
				attackTarget(target)
				break
			end
				if lastPos then
					hum:MoveTo(lastPos)
					hum.MoveToFinished:Wait()
					lastPos = nil
					break
				else
					hum:MoveTo(waypoint.Position)
					hum.MoveToFinished:Wait()
				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 wait(.5) do
	patrol()
end


--ERRORS
--16 cannot cast to object

u should probably pick a different category.

1 Like

You can’t filter RaycastParams itself, as you’re supposed to put objects that you want to filter. For example, you can filter the variable bear, but you can’t filter the RaycastParams itself.