Let me take an earlier question to the next level. Let’s say I have 3 parts in a folder. I want to use 1 script to constantly change the transparency of all 3 of those parts over and over. I’ve written the script below, thinking it would affect all the parts, but it’s only changing the transparency of 1 of the parts. I’m assuming something is missing to allow all parts to be affected independently. Here is my attempted script. I could write a script for each part, but that would be excessive. Any thoughts?
local glassTable = script.Parent.GlassFolder:GetChildren()
for _, glass in pairs(glassTable) do
while true do
for i = 0,1,.1 do
glass.Transparency = i
wait(.1)
end
end
end
This is because the for _ loop has looped once, and is now running a indefinite while true do loop on this one part it found. What you can do instead is
while true do
for _, glass in pairs(glassTable) do
for i = 0,1,.1 do
glass.Transparency = i
wait(.1)
end
end
wait()
end
I did a bit more digging around and came across the spawn(function()). I was able to make it work by making use of it. Does this seem like an appropriate/efficient way to get the result?
local glassTable = script.Parent.GlassFolder:GetChildren()
for _, glass in pairs(glassTable) do
spawn(function()
while true do
for i = 0,1,.1 do
glass.Transparency = i
wait(.1)
end
end
end)
end
This is a way to do it, however you’re creating a new thread every loop which isn’t very good for optimization. This means you’re running two scripts instead of one, and creating one every loop as well.
Your script did work as well, but with a different, unexpected outcome (unexpected in my mind). In your code the blocks change transparency, one block at a time, then the next block is affected. The updated code I wrote using spawn has each block changing transparency at the same time. Cool to see different outcomes.
local tweenInfo = TweenInfo.new(
2, -- Time
Enum.EasingStyle.Linear, -- EasingStyle
Enum.EasingDirection.Out, -- EasingDirection
-1, -- RepeatCount (when less than zero the tween will loop indefinitely)
true, -- Reverses (tween will reverse once reaching it's goal)
0 -- DelayTime
)
You would then create a tween using this tweeninfo like so
local tween = TweenService:Create(glass, tweenInfo, {Transparency = 1})
local TweenService = game:GetService("TweenService")
local tweenInfo = TweenInfo.new(1, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, -1, true, 1)
-- time, easingStyle, easingDirection, repeats, reverses, delay
for _, part in pairs(script.Parent.GlassFolder:GetChildren()) do
TweenService:Create(part, tweenInfo, {Transparency = 1}):Play()
end
Nice! Your tween script produced the same effect. I take it that such scripting will be more efficient in producing the same result. Now I have a bit of reading and pondering to better understand this service.
I made use of the tween script to add to a simple model I’m working on. I’m trying to get the appearance of a mildly changing screen/glass area. Here is a video just showing how it looks with the tween script applied. There are 20 glass blocks there stacked on top of each other. You can tell that it’s not quite a perfect change in time, but I still think it’s a success.