Alright.. So I'm new to programming and I'm wondering, how would I shorten this script..?
What I'm trying to achieve is to change the transparency of a texture as a way to present different advertisements.
My script should explain it all.
Script:
AdvertisementBoards = game.Workspace.AdvertisementBoards Part = AdvertisementBoards.Part Part2 = AdvertisementBoards.Part2 Part3 = AdvertisementBoards.Part3 Part4 = AdvertisementBoards.Part4 Part5 = AdvertisementBoards.Part5 WaitTime = 0.01 while wait() do Part.BloxyCola.Transparency = 0 Part2.BloxyCola.Transparency = 0 Part3.BloxyCola.Transparency = 0 Part4.BloxyCola.Transparency = 0 Part5.BloxyCola.Transparency = 0 wait(45) Part.BloxyCola.Transparency = 0.05 Part2.BloxyCola.Transparency = 0.05 Part3.BloxyCola.Transparency = 0.05 Part4.BloxyCola.Transparency = 0.05 Part5.BloxyCola.Transparency = 0.05 wait(WaitTime) Part.BloxyCola.Transparency = 0.1 Part2.BloxyCola.Transparency = 0.1 Part3.BloxyCola.Transparency = 0.1 Part4.BloxyCola.Transparency = 0.1 Part5.BloxyCola.Transparency = 0.1 wait(WaitTime) Part.BloxyCola.Transparency = 0.15 Part2.BloxyCola.Transparency = 0.15 Part3.BloxyCola.Transparency = 0.15 Part4.BloxyCola.Transparency = 0.15 Part5.BloxyCola.Transparency = 0.15 wait(WaitTime) Part.BloxyCola.Transparency = 0.2 Part2.BloxyCola.Transparency = 0.2 Part3.BloxyCola.Transparency = 0.2 Part4.BloxyCola.Transparency = 0.2 Part5.BloxyCola.Transparency = 0.2 wait(WaitTime) Part.BloxyCola.Transparency = 0.25 Part2.BloxyCola.Transparency = 0.25 Part3.BloxyCola.Transparency = 0.25 Part4.BloxyCola.Transparency = 0.25 Part5.BloxyCola.Transparency = 0.25 wait(WaitTime) Part.BloxyCola.Transparency = 0.3 Part2.BloxyCola.Transparency = 0.3 Part3.BloxyCola.Transparency = 0.3 Part4.BloxyCola.Transparency = 0.3 Part5.BloxyCola.Transparency = 0.3 wait(WaitTime) Part.BloxyCola.Transparency = 0.35 Part2.BloxyCola.Transparency = 0.35 Part3.BloxyCola.Transparency = 0.35 Part4.BloxyCola.Transparency = 0.35 Part5.BloxyCola.Transparency = 0.35 wait(WaitTime) Part.BloxyCola.Transparency = 0.4 Part2.BloxyCola.Transparency = 0.4 Part3.BloxyCola.Transparency = 0.4 Part4.BloxyCola.Transparency = 0.4 Part5.BloxyCola.Transparency = 0.4 wait(WaitTime) Part.BloxyCola.Transparency = 0.45 Part2.BloxyCola.Transparency = 0.45 Part3.BloxyCola.Transparency = 0.45 Part4.BloxyCola.Transparency = 0.45 Part5.BloxyCola.Transparency = 0.45 wait(WaitTime) Part.BloxyCola.Transparency = 0.5 Part2.BloxyCola.Transparency = 0.5 Part3.BloxyCola.Transparency = 0.5 Part4.BloxyCola.Transparency = 0.5 Part5.BloxyCola.Transparency = 0.5 wait(WaitTime) Part.BloxyCola.Transparency = 0.55 Part2.BloxyCola.Transparency = 0.55 Part3.BloxyCola.Transparency = 0.55 Part4.BloxyCola.Transparency = 0.55 Part5.BloxyCola.Transparency = 0.55 wait(WaitTime) Part.BloxyCola.Transparency = 0.6 Part2.BloxyCola.Transparency = 0.6 Part3.BloxyCola.Transparency = 0.6 Part4.BloxyCola.Transparency = 0.6 Part5.BloxyCola.Transparency = 0.6 wait(WaitTime) Part.BloxyCola.Transparency = 0.65 Part2.BloxyCola.Transparency = 0.65 Part3.BloxyCola.Transparency = 0.65 Part4.BloxyCola.Transparency = 0.65 Part5.BloxyCola.Transparency = 0.65 wait(WaitTime) Part.BloxyCola.Transparency = 0.7 Part2.BloxyCola.Transparency = 0.7 Part3.BloxyCola.Transparency = 0.7 Part4.BloxyCola.Transparency = 0.7 Part5.BloxyCola.Transparency = 0.7 wait(WaitTime) Part.BloxyCola.Transparency = 0.75 Part2.BloxyCola.Transparency = 0.75 Part3.BloxyCola.Transparency = 0.75 Part4.BloxyCola.Transparency = 0.75 Part5.BloxyCola.Transparency = 0.75 wait(WaitTime) Part.BloxyCola.Transparency = 0.8 Part2.BloxyCola.Transparency = 0.8 Part3.BloxyCola.Transparency = 0.8 Part4.BloxyCola.Transparency = 0.8 Part5.BloxyCola.Transparency = 0.8 wait(WaitTime) Part.BloxyCola.Transparency = 0.85 Part2.BloxyCola.Transparency = 0.85 Part3.BloxyCola.Transparency = 0.85 Part4.BloxyCola.Transparency = 0.85 Part5.BloxyCola.Transparency = 0.85 wait(WaitTime) Part.BloxyCola.Transparency = 0.9 Part2.BloxyCola.Transparency = 0.9 Part3.BloxyCola.Transparency = 0.9 Part4.BloxyCola.Transparency = 0.9 Part5.BloxyCola.Transparency = 0.9 wait(WaitTime) Part.BloxyCola.Transparency = 0.95 Part2.BloxyCola.Transparency = 0.95 Part3.BloxyCola.Transparency = 0.95 Part4.BloxyCola.Transparency = 0.95 Part5.BloxyCola.Transparency = 0.95 wait(WaitTime) Part.BloxyCola.Transparency = 1 Part2.BloxyCola.Transparency = 1 Part3.BloxyCola.Transparency = 1 Part4.BloxyCola.Transparency = 1 Part5.BloxyCola.Transparency = 1 wait(WaitTime) Part.LegendaryStudios.Transparency = 0.95 Part2.LegendaryStudios.Transparency = 0.95 Part3.LegendaryStudios.Transparency = 0.95 Part4.LegendaryStudios.Transparency = 0.95 Part5.LegendaryStudios.Transparency = 0.95 wait(WaitTime) Part.LegendaryStudios.Transparency = 0.9 Part2.LegendaryStudios.Transparency = 0.9 Part3.LegendaryStudios.Transparency = 0.9 Part4.LegendaryStudios.Transparency = 0.9 Part5.LegendaryStudios.Transparency = 0.9 wait(WaitTime) Part.LegendaryStudios.Transparency = 0.85 Part2.LegendaryStudios.Transparency = 0.85 Part3.LegendaryStudios.Transparency = 0.85 Part4.LegendaryStudios.Transparency = 0.85 Part5.LegendaryStudios.Transparency = 0.85 wait(WaitTime) Part.LegendaryStudios.Transparency = 0.8 Part2.LegendaryStudios.Transparency = 0.8 Part3.LegendaryStudios.Transparency = 0.8 Part4.LegendaryStudios.Transparency = 0.8 Part5.LegendaryStudios.Transparency = 0.8 wait(WaitTime) Part.LegendaryStudios.Transparency = 0.75 Part2.LegendaryStudios.Transparency = 0.75 Part3.LegendaryStudios.Transparency = 0.75 Part4.LegendaryStudios.Transparency = 0.75 Part5.LegendaryStudios.Transparency = 0.75 wait(WaitTime) Part.LegendaryStudios.Transparency = 0.7 Part2.LegendaryStudios.Transparency = 0.7 Part3.LegendaryStudios.Transparency = 0.7 Part4.LegendaryStudios.Transparency = 0.7 Part5.LegendaryStudios.Transparency = 0.7 wait(WaitTime) Part.LegendaryStudios.Transparency = 0.65 Part2.LegendaryStudios.Transparency = 0.65 Part3.LegendaryStudios.Transparency = 0.65 Part4.LegendaryStudios.Transparency = 0.65 Part5.LegendaryStudios.Transparency = 0.65 wait(WaitTime) Part.LegendaryStudios.Transparency = 0.6 Part2.LegendaryStudios.Transparency = 0.6 Part3.LegendaryStudios.Transparency = 0.6 Part4.LegendaryStudios.Transparency = 0.6 Part5.LegendaryStudios.Transparency = 0.6 wait(WaitTime) Part.LegendaryStudios.Transparency = 0.55 Part2.LegendaryStudios.Transparency = 0.55 Part3.LegendaryStudios.Transparency = 0.55 Part4.LegendaryStudios.Transparency = 0.55 Part5.LegendaryStudios.Transparency = 0.55 wait(WaitTime) Part.LegendaryStudios.Transparency = 0.5 Part2.LegendaryStudios.Transparency = 0.5 Part3.LegendaryStudios.Transparency = 0.5 Part4.LegendaryStudios.Transparency = 0.5 Part5.LegendaryStudios.Transparency = 0.5 wait(WaitTime) Part.LegendaryStudios.Transparency = 0.45 Part2.LegendaryStudios.Transparency = 0.45 Part3.LegendaryStudios.Transparency = 0.45 Part4.LegendaryStudios.Transparency = 0.45 Part5.LegendaryStudios.Transparency = 0.45 wait(WaitTime) Part.LegendaryStudios.Transparency = 0.4 Part2.LegendaryStudios.Transparency = 0.4 Part3.LegendaryStudios.Transparency = 0.4 Part4.LegendaryStudios.Transparency = 0.4 Part5.LegendaryStudios.Transparency = 0.4 wait(WaitTime) Part.LegendaryStudios.Transparency = 0.35 Part2.LegendaryStudios.Transparency = 0.35 Part3.LegendaryStudios.Transparency = 0.35 Part4.LegendaryStudios.Transparency = 0.35 Part5.LegendaryStudios.Transparency = 0.35 wait(WaitTime) Part.LegendaryStudios.Transparency = 0.3 Part2.LegendaryStudios.Transparency = 0.3 Part3.LegendaryStudios.Transparency = 0.3 Part4.LegendaryStudios.Transparency = 0.3 Part5.LegendaryStudios.Transparency = 0.3 wait(WaitTime) Part.LegendaryStudios.Transparency = 0.25 Part2.LegendaryStudios.Transparency = 0.25 Part3.LegendaryStudios.Transparency = 0.25 Part4.LegendaryStudios.Transparency = 0.25 Part5.LegendaryStudios.Transparency = 0.25 wait(WaitTime) Part.LegendaryStudios.Transparency = 0.2 Part2.LegendaryStudios.Transparency = 0.2 Part3.LegendaryStudios.Transparency = 0.2 Part4.LegendaryStudios.Transparency = 0.2 Part5.LegendaryStudios.Transparency = 0.2 wait(WaitTime) Part.LegendaryStudios.Transparency = 0.15 Part2.LegendaryStudios.Transparency = 0.15 Part3.LegendaryStudios.Transparency = 0.15 Part4.LegendaryStudios.Transparency = 0.15 Part5.LegendaryStudios.Transparency = 0.15 wait(WaitTime) Part.LegendaryStudios.Transparency = 0.1 Part2.LegendaryStudios.Transparency = 0.1 Part3.LegendaryStudios.Transparency = 0.1 Part4.LegendaryStudios.Transparency = 0.1 Part5.LegendaryStudios.Transparency = 0.1 wait(WaitTime) Part.LegendaryStudios.Transparency = 0.05 Part2.LegendaryStudios.Transparency = 0.05 Part3.LegendaryStudios.Transparency = 0.05 Part4.LegendaryStudios.Transparency = 0.05 Part5.LegendaryStudios.Transparency = 0.05 wait(WaitTime) Part.LegendaryStudios.Transparency = 0 Part2.LegendaryStudios.Transparency = 0 Part3.LegendaryStudios.Transparency = 0 Part4.LegendaryStudios.Transparency = 0 Part5.LegendaryStudios.Transparency = 0 wait(45) end
You should look into the principles of iteration. What you're looking for here is a series of for loops
, I believe.
I would also like to add that using while wait() do
is very bad practice. I see a lot of developers doing this. They look at it, they think "ah that's fancy". It's not, it's bad practice. The reason for this being bad is because you're relying on the fact wait() returns a number, which is a "truth" value. Therefore, while wait() do technically doesn't fail. If for whatever reason the implementation of wait() changed, and it returned nil instead, this is going to fail immediately and the loop will never work. Imagine you have multiple of these across your game and many games, everything will break. You want full control of your condition. You don't want some global function controlling that - it could change at any time. It's bad code for many reasons.
As for your code, I wrote something which I'm not sure if it works, but here goes it:
local AdvertisementBoards = game.Workspace.AdvertisementBoards:GetChildren() -- This will return a table {} of all of the children in AdvertisementBoards local WaitTime = 0.01 local function fade(obj, args) local i,ii,iii = unpack(args) -- from, to, increment for val = i,ii,iii do obj.Transparency = val wait(WaitTime) end end while true do for _, part in ipairs(AdvertisementBoards) do coroutine.wrap(function() part.BloxyCola.Transparency = 1 wait(45) fade(part.BloxyCola, {0, 1, 0.05}) fade(part.LegendaryStudios, {0.95, 0, -0.05}) end)() end wait(45) end