Make nearby parts transparent?


I’m making a game for picking fruit.
i want all nearby leaves to be semi-transparent near the player so they can see the fruit underneath
does anyone know how to do this?


1 Like

I would add a hitbox-sort of contraption to the player that when the leaves detect (.hit) that part they turn transparent. Scale the hitbox part to whatever distance you want the leaves to be and make it to where when the hitbox is not touching (.hit again) the leaves they revert to their original transparency. I would recommend putting the leaves into a single model or maybe even creating a hitbox for the leaves too for simplicity.

1 Like

Update: This method is not optimized, and so I’ll be looking for more solutions.

Hmmm, the only other viable option I could think of would be a raycast, but that would seem just as unoptimized.

local distance = 16

-- Get a reference to the local player
local player = game.Players.LocalPlayer

-- Create a function that will make parts transparent
local function makeTransparent(part)
  part.Transparency = 1

-- Create a function that will be called on each iteration of the loop
local function onLoopIteration()
  -- Find all parts within the specified distance of the player
  local parts = game.Workspace:FindPartsInRegion3(
      player.Character.Head.Position -, distance, distance),
      player.Character.Head.Position +, distance, distance)

  -- Make each part transparent
  for i = 1, #parts do

-- Create a loop that will call the onLoopIteration function
local loop = game:GetService("RunService").RenderStepped:Connect(onLoopIteration)

You can change stuff to your likings.

is this optimized? I need optimized systems but dont know if region3s are well optimized.

It is as optimized as it gets;

If I were to optimize this this I would do 2 things: use heartbeat instead of renderstepped and use magnitude checks instead of region3.

is magnitude more optimized due to less calculations?

Could you show me an example? <<<<<

something like this?

local Chracter = game.Players.localPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()

	for i, Part in ipairs(workspace:GetDescendants()) do
		if not Part:IsA("BasePart") then continue end
		if Part:IsDescendantOf(Chracter) then continue end
		if (Part.Position - Chracter.PrimaryPart.Position).Magnitude < 16 then
			Part.Transparency = 1

hmmmmmm okay
I see.
Is workspace:GetDescendants optimized?

If there are many parts, and also running it at high speeds is extremely laggy.

can I just call :get children on workspace before the heartbeat function, store it to a table, then use it over and over

instead of GetDescendants I would recommend using CollectionService, and loop through that, as then you are only getting parts that need to be changed.

explain more plz
I am really interested to hear about this

I have never used collection service before, but I managed to do something with it.

local RunService = game:GetService("RunService")
local CollectionService= game:GetService("CollectionService")
local Player = game.Players.localPlayer

function TagPart(Part)
    if not Part:IsA("BasePart") or Part:IsDescendantOf(Player.Character) then return end
    CollectionService:AddTag(Part, "Part")

for i, Part in ipairs(workspace:GetDescendants()) do


    for i, Part in ipairs(CollectionService:GetTagged("Part")) do
        if (Part.Position - Player.Chracter.PrimaryPart.Position).Magnitude > 16 then continue end
        Part.Transparency = 1

It would be most efficient if you were to split chunks of leaves up into an array of cells. You could then simply index cells near the player via their position.

This would technically give you as many leaves as you want, since you no longer have to loop through every single instance.

I quickly wrote an example script in case you want to try it. (The script is located in StarterCharacterScripts):
grid_of_leaves.rbxl (51.0 KB)

I mean
to make every single one bush transparent
dont I have to do a for loop even as an array

When you want to alter multiple instances at once, it becomes difficult finding a solution which does not require looping through an array.

Arrays are important (and fast!). The loop in the example place I had sent iterated through 200 instances of leaves, and, at least on my system, ultimately had an average performance cost of 0.0063 seconds.