Raycasting wall thing

Hi. I’m trying to make a piggy game and make it so when the player goes behind a wall, the bot will go and walk away. But I don’t how to get the target for the rayCast parameter.

Here is my script:

local function rayCast(target)
	local body = target.Parent
	
	local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {Bot, body}
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
	
	local raycastResult = workspace:Raycast(humRootPart.Position, target.Position, raycastParams)

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

local function findTarget()
	local distance = 33
	local target = nil
	for i, v in pairs(game.Workspace:GetChildren()) do
		local human = v:FindFirstChild("Humanoid")
		local humanoidRootPart = v:FindFirstChild("HumanoidRootPart")
		if human and humanoidRootPart and v ~= script.Parent then
			if (humanoidRootPart.Position - humRootPart.Position).magnitude < distance then
				distance = (humanoidRootPart.Position - humRootPart.Position).magnitude
				target = humanoidRootPart	
			end
		end
	end
	return target, distance
end

You’d have to iterate all of the players’ HumanoidRootParts, then cast a ray from the enemy’s HRP in the direction of the current player’s HRP.

local enemyHrp = workspace.Enemy.HumanoidRootPart -- this can be anything, wasn't sure how you were going to reference it

local distance = 33
local availablePlayers = {}
for i,v in pairs(game:GetService('Players'):GetPlayers() do
    local character = v.Character
    if character then -- if their character doesn't exist, we shouldn't take it into account
        local humanoidRootPart = character:FindFirstChild('HumanoidRootPart')
        if humanoidRootPart then -- if the HRP doesn't exist, the character doesn't exist or isn't fully loaded
            if (humanoidRootPart - enemyHRP.Position) <= distance then
                workspace:Raycast(enemyHRP.Position, humanoidRootPart.Position - humanoidRootPart) -- this would be the function that casts the ray from the enemy's HRP in the direction of the players' HRP.
            end
        end
    end
end

If you try to make the Bot ignore the wall or door, try to use CollectionService