Editing a model's parts' transparency values all at once using a script

I’m currently making a club and I’m doing something that makes it so that the music loudness effects how transparent a few parts that are neon are. I have made the rest of the script, I just need to figure out how to select all of the children so I can edit their transparency all together. I’ve already searched for this issue and nothing has come up, I’ve also tried using :GetAllChildren() to edit everything and that doesn’t work. I just can’t think of how to do it, can someone help?
image
The “NeonTrimming” in there is the model. This script is only selecting the model right now, not the parts.

3 Likes

You may want to use :GetDescendants() instead of :GetChildren().
Example code:

local function SetModelTransparency(Model,Transparency)
	for _,Part in pairs(Model:GetDescendants()) do
		if Part:IsA("BasePart") then
			Part.Transparency = Transparency
		end
	end
end

local Model1 = game.Workspace.DAUnderStage
local Model2 = game.Workspace.NeonTrimming
local Sound = game.Workspace.MusicValues.MusicSound
while true do
	local Loudness = Sound.PlaybackLoudness/1000
	Loudness = 1 - Loudness - 0.3
	SetModelTransparency(Model1,Loudness)
	SetModelTransparency(Model2,Loudness)
	wait()
end
13 Likes

To expand on this, if this can be a pretty heavy operation:
There are things you can do to help alleviate some performance.

We’re repeating quite a bit of work filtering out objects as baseparts everytime we would want to call this function. If it is going to be used in a loop or as lerped tween we would want this to skip as many steps as possible.

Additionally, calling :IsA on every descendant can be anywhere from 1 object to thousands of objects (ouch)
Since IsA is a lua binding to a C++ function it can be a source of slowdown if used frequently
You can cut two C++ bindings per transparency if you cache the objects you want to edit:

local baseParts = {}

for _, object in ipairs(model:GetDescendants()) do 
	if object:IsA("BasePart") then
		table.insert(baseParts, object)
	end
end

local function setTransparency(goal)
	for _, basePart in ipairs(baseParts) do
		basePart.Transparency = goal --Still reflected to the C++ layer, but C++ reflection is now O(n) instead of O(2n)+1
	end
end

This can be used an an exercise to be mindful of when you pass data over reflection and what work can be done upfront proactively.
Even if these functions werent being replicated to C++ and back, you’ll still avoid redoing work you’ve previously done which can be big difference in performance and how you architect your code.

However at the same time, if you’re going to call this function once and never again, there’s nothing to be gained to cache these objects and you would rather want to avoid using up any memory with this approach.

6 Likes

You could also check their ClassNames and that would be faster. Right?

So

if thing.ClassName == “Part” then
2 Likes

IsA is nice because it will also compare against an objects superclasses.

BasePart will cover the following:

  • Part
  • WedgePart
  • UnionOperation
  • MeshPart
  • MaybeSomeOtherPartInTheFuture
Object Browser and SuperClasses

09%20AM
You can check out other super classes in the Object Browser (the current wiki currently doesnt give a great way to view these)

ClassName lookup still has to be reflected but may be marginally faster since it doesnt have to be invoked as a function. Someone would have to measure the time it takes to lookup but its probably not worth making the change from one to the other, especially since both comparisons have different behavior. At that point we miiiiiight be microptomizing at the cost of less readable code.

6 Likes

Yeah. I never bother to “optimize” my code to such a point, and it is completely redundant. But some people care about that stuff ¯ _(ツ)_/¯

2 Likes

Thank you so very much, this works beautifully in my game!

Thank you for the suggestion! I’m fairly new to ROBLOX Lua scripting so I don’t think that I’ll be using complex functions quite yet. I also don’t think that it will cause any lag in the game as a whole, but I’ll be sure to refer back to this again if I have any issues, thanks again! <3

2 Likes