Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I make this script shorter/neater?

I'm making a main menu that has a lot of tweens. How can I make this neater if possible? There's a lot of repetitions and I thought about the "don't repeat yourself" rule. I think you can use tables but I don't know how to use them in this situation. Here's my script:

local tween1 = TS:Create(frame.Frame.UIGradient,tweenInfo,properties1)
local tween2 = TS:Create(frame.Frame.TextButton1.TextLabel,tweenInfo2,properties2)
local tween3 = TS:Create(frame.Frame.TextButton2.TextLabel,tweenInfo2,properties2)
local tween4 = TS:Create(frame.Frame.TextButton3.TextLabel,tweenInfo2,properties2)
local tween5 = TS:Create(frame.Frame.TextButton4.TextLabel,tweenInfo2,properties2)

frame.Changed:Connect(function(property)
	if not property == "Visible" then
		return
	end
	wait(1)
	tween1:Play()
	tween1.Completed:Wait()
	tween2:Play()
	tween2.Completed:Wait()
	tween3:Play()
	tween3.Completed:Wait()
	tween4:Play()
	tween4.Completed:Wait()
	tween5:Play()
	tween5.Completed:Wait()
end)

2 comments

0

you can do all this in 1 line but it would look odd like this: local tween1,tween2,tween3,tween4,tween5 = (put all your tweens here)

0

Ima play around with the code and see if I can get a function going that just needs a few inputs, ill respond with an answer If I get it.

1 answer

✓ Accepted answer
local tween1 = TS:Create(frame.Frame.UIGradient,tweenInfo,properties1)
local tween2 = TS:Create(frame.Frame.TextButton1.TextLabel,tweenInfo2,properties2)
local tween3 = TS:Create(frame.Frame.TextButton2.TextLabel,tweenInfo2,properties2)
local tween4 = TS:Create(frame.Frame.TextButton3.TextLabel,tweenInfo2,properties2)
local tween5 = TS:Create(frame.Frame.TextButton4.TextLabel,tweenInfo2,properties2)
-- This is as short as you can get the top lines, as each one has to be defined seperately, however you can shorten the function below
frame.Changed:Connect(function(property)
	if not property == "Visible" then
		return
	end
	wait(1)
	tween1:Play()
	tween1.Completed:Wait()
	tween2:Play()
	tween2.Completed:Wait()
	tween3:Play()
	tween3.Completed:Wait()
	tween4:Play()
	tween4.Completed:Wait()
	tween5:Play()
	tween5.Completed:Wait()
end)


--Shorter version
local tweens = {}
for i=1, 5 do
	table.insert(tweens,"Tween"..i)
end

frame.Changed:Connect(function(property)
	if not property == "Visible" then
		for i,v in pairs(tweens) do
			v:Play()
			v.Completed:Wait()
		end
	end
end)

--I have not tested this, but it should work. It basically just adds all the tweens into a table so you can just run a loop to play through them all instead of having to have a new line for each.

This discussion is preserved as it was. New answers, comments, and votes are closed.