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
...
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
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
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
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