Multiple for i, v in pairs not working with raycast

Hello,
Not too long ago someone helped me with raycasts for my game, and whats it does is take when youre not in frame and cant see your character, it makes a building go half invisible. It works fine with one building, but adding another it works weirdly. Unsure what the problem is. Heres a video showcasing the problem:

As you can see, one of the buildings is always half invisible, and the other works fine.

Heres the code in a local script:

local Player = game.Players.LocalPlayer
local Character = Player.Character
local Camera = workspace.Camera

local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Whitelist --> Only the parts in "FilterDescendantsInstances" will be considered
params.FilterDescendantsInstances = {workspace.house:GetChildren(), workspace.your:GetChildren()} --> Walls go in here

game:GetService("RunService").RenderStepped:Connect(function() --> Runs every frame
	local cameraPos = Camera.CFrame.Position --> Getting the camera position
	local characterPos = Character:GetPrimaryPartCFrame().Position --> Getting the character position
	local raycastResult = workspace:Raycast(cameraPos, (characterPos - cameraPos), params)
	if raycastResult then --> A wall intercepts the camera and the character
		for i, v in pairs(workspace.house:GetChildren()) do
			if v:IsA("BasePart") then
				v.Transparency = 0.5
			end
		end
		for i, v in pairs(workspace.your:GetChildren()) do
			if v:IsA("BasePart") then
				v.Transparency = 0.5
			end
		end


		
	else
		for i, v in pairs(workspace.house:GetChildren()) do
			if v:IsA("BasePart") then
				v.Transparency = 0
			end
		end
		for i, v in pairs(workspace.your:GetChildren()) do
			if v:IsA("BasePart") then
				v.Transparency = 0.5
			end
		end


	end
end)
```i

It’s because the script is only referencing one of the two houses? You’re directly indexing for just one of the houses from the workspace via workspace.house

Ideally you want to perhaps organize the parts of the houses into a table and then go on from there

Also it is SUPER REDUNDANT to use :GetChildren for each raycast EVERY SINGLE FRAME, unless the house is going to change by adding or removing parts the part list should be a static table for optimization reasons

Example:

--at the top of the script
local parts = {}
for _, v in ipairs(workspace:GetChildren()) do
  if v.Name == 'house' then
    for _, w in ipairs(v:GetChildren()) do
      if w:IsA('BasePart') then
        table.insert(parts, w)
      end
    end
  end
end
...
--Renderstepped loop

if raycastResult then
  for _, v in ipairs(parts) do
    v.Transparency = .5
...

But how would I make it so that theres another model in the table, the your model? That doesn’t help.

You do the same thing for the your model???

local parts = {}
for _, v in ipairs(workspace:GetChildren()) do
  if v.Name == 'house' then
    for _, w in ipairs(v:GetChildren()) do
      if w:IsA('BasePart') then
        table.insert(parts, w)
      end
    end
  end
end
for _, v in ipairs(workspace.your:GetChildren()) do
  if v:IsA('BasePart') then
    table.insert(parts, v)
  end
end

Tested it, when one of the houses is in frame it makes both of them invisible, not just the one in view.

sorry my bad im a little dumb

You should’ve used the raycastResult to get the house that is obstructing the view, and go on from there

if raycastResult then
  local house = raycastResult.Instance.Parent
  for i, v in pairs(house:GetChildren()) do...

Alright, it works becoming transparent, but I tried making it not transparent when it becomes out of frame but it didn’t work. Here’s what I did:

else
		local house = raycastResult.Instance.Parent
		for i, v in pairs(house:GetChildren()) do
			if v:IsA("BasePart") then
				v.Transparency = 0
			end
		end
		local your = raycastResult.Instance.Parent
		for i, v in pairs(your:GetChildren()) do
			if v:IsA("BasePart") then
				v.Transparency = 0
			end
		end

And it doesn’t work. Keeps printing the error "attempt to index nil with ‘Instance’ " on the line

local house = raycastResult.Instance.Parent

Forgot to reply to the right post my bad

it doesn’t work because that section of the if statement is when the raycastResult is nil, which it obviously is and thus you can’t use it

there’s also going to be this weird edge case where your view is obstructed by multiple structures, so using raycast for this might not be ideal at all

and for that I present to you a cool new function: workspace:GetPartsBoundsInBox()
it basically just gives you all of the parts that are inside a given hitbox of some sort, i like to think it as raycast but it detects everything in its path instead of just one

here’s footage:


and here’s the place file:
i made this at 2 am in the morning.rbxl (30 KB)

and here’s a diagram:

Disclaimer: the coding is absolute crap, i made it in a hurry, I do not expect you to just literally copy and paste the code into your game, it’s just for demonstration purposes so you get the idea of how to use it

if u have any questions about these funny functions u can just look them up on the API reference

Alright, I messed around with it and got it to work, thanks!