Is there a way to tween this gui better?

Hey I wrote this terrible code and i was wondering if there is anyway i could make this code better

…
local frame = script.Parent

local guiltrigger = game:GetService(“ReplicatedStorage”).guitrigger

local tweenservice = game:GetService(“TweenService”)
local tweeninfo = TweenInfo.new(
0.1,
Enum.EasingStyle.Sine,
Enum.EasingDirection.In,
0,
false,
0
)

guiltrigger.OnClientEvent:Connect(function(translevel, triggerID)

if triggerID == 1 then
	for i, v in ipairs(script.Parent:GetChildren()) do
		if v:IsA("TextButton") then
			local tweenx = tweenservice:Create(v, tweeninfo, {BackgroundTransparency = translevel, TextTransparency = translevel})
			tweenx:Play()
			for a, b in ipairs(v:GetChildren()) do
				if b:IsA("UIStroke") then
					local tweenx = tweenservice:Create(b, tweeninfo, {Transparency = translevel})
					tweenx:Play()
				end
			end
		elseif v:IsA("ImageLabel") then
			local tweenx = tweenservice:Create(v, tweeninfo, {ImageTransparency = translevel})
			tweenx:Play()
		elseif v:IsA("Frame") then
			local tweenx = tweenservice:Create(v, tweeninfo, {BackgroundTransparency = translevel})
			tweenx:Play()
			for a, b in ipairs(v:GetChildren()) do
				if b:IsA("UIStroke") then
					local tweenx = tweenservice:Create(b, tweeninfo, {Transparency = translevel})
					tweenx:Play()
				elseif b:IsA("ImageLabel") then
					local tweenx = tweenservice:Create(b, tweeninfo, {ImageTransparency = translevel})
					tweenx:Play()
				elseif b:IsA("TextLabel") then
					local tweenx = tweenservice:Create(b, tweeninfo, {TextTransparency = translevel})
					tweenx:play()
				end
			end
		end
	end
end

end)
..

So you want it beautified?

local frame = script.Parent
local guiltrigger = game:GetService("ReplicatedStorage").guitrigger
local tweenservice = game:GetService("TweenService")
local tweeninfo = TweenInfo.new(0.1, Enum.EasingStyle.Sine, Enum.EasingDirection.In)
local props = nil

function applyTransparency(inst, t)
	if inst:IsA("TextButton") then props = {BackgroundTransparency = t, TextTransparency = t}
	elseif inst:IsA("TextLabel") then props = {TextTransparency = t}
	elseif inst:IsA("ImageLabel") then props = {ImageTransparency = t}
	elseif inst:IsA("Frame") then props = {BackgroundTransparency = t}
	elseif inst:IsA("UIStroke") then props = {Transparency = t} end
	if props then tweenservice:Create(inst, tweeninfo, props):Play() end
	for _, child in ipairs(inst:GetChildren()) do applyTransparency(child, t) end
end

guiltrigger.OnClientEvent:Connect(function(translevel, triggerID)
	if triggerID == 1 then
		for _, child in ipairs(frame:GetChildren()) do
			applyTransparency(child, translevel)
		end
	end
end)
local props = {"BackgroundTransparency", "TextTransparency", "ImageTransparency", "Transparency"}

function applyTransparency(inst, t)
	for _, v in props do pcall(function() inst[v] = t end) end
	for _, child in ipairs(inst:GetChildren()) do applyTransparency(child, t) end
end

Could also use a lookup table, for further cleanliness


local propertyNames = {
	TextButton = {"BackgroundTransparency", "TextTransparency"},
	TextLabel = {"TextTransparency"},
	ImageLabel = {"ImageTransparency"},
	Frame = {"BackgroundTransparency"},
	UIStroke = {"Transparency"},
}

function applyTransparency(inst, t)
	local props = propertyNames[inst.ClassName]
	if not props then return end -- This return would prevent the function from acting on gui objects that are children of odd instances

	local propsGoal = {}
	for _, s in ipairs(props) do
		propsGoal[s] = t
	end

	tweenservice:Create(inst, tweeninfo, propsGoal):Play()

	for _, child in ipairs(inst:GetChildren()) do 
		applyTransparency(child, t) 
	end
end

ABOLISH if statements

Nice.. I like to stick with what they wrote more or less.
Believe me I could make that so tight you couldn’t even read it. :rofl:

Last time I checked, OP wanted to tween more than 1 property for some classes

1 Like

fixed :P

Might be more verbose, but it’s easy to read, and easy to adapt

2 Likes