Make nearby parts transparent?

Hi,

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?

Sincerely,
-coolguyweri

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
end

-- 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(
    Region3.new(
      player.Character.Head.Position - Vector3.new(distance, distance, distance),
      player.Character.Head.Position + Vector3.new(distance, distance, distance)
    ),
    math.huge
  )

  -- Make each part transparent
  for i = 1, #parts do
    makeTransparent(parts[i])
  end
end

-- 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()

game:GetService("RunService").Heartbeat:Connect(function()
	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
		end
	end
end)

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")
end

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

workspace.DescendantAdded:Connect(TagPart)

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

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.