Creating tweens for multiple Gui elements

Hi, today I want to make a GUI that has tweens, but it has a lot of texts and I don’t want to make the tweens one by one, if you didn’t understand what I want to do, here is an example:

Let’s say I have a TweenInfo and a Goal, and when I do TweenService:Create(part), I want the same values of TweenInfo and the Goal to be applied to the box where I put part.

And that’s what I want to do, tweet multiple parts with the same value of TweenInfo and Goal, thank you for helping me!

I want answers related to this topic.
Sorry, I’m bad explaining.
If you need more information, don’t hesitate to leave it in the answers and I’ll answer you!

Wait, so I suppose you want to tween like for example 6 parts to different positions right?

1 Like

No, same position.

TAGS: Heart button not working…

So basically you want all the TextLabels to Tween from example: 0,0,1,0 to 0,0,0,0? At the same time.

3 Likes

Yeah, I want to that happen!

TAGS: Heart button not working…

Okay then you can do something like:

for i,v in pairs(Texts:GetChildren()) do
spawn(function()
game:GetService("TweenService"):Create(v, TweeInfo.new(1)--[[amount of time--]], {Position = UDim2.new(0,0,0,0--[[Position here]]}):Play()
end)
end
2 Likes

Can you explain what spawn() does?

It creates a the same thing for all the texts, if you don’t it would work just for 1 text.

Please don’t use spawn. It’s not necessary either as you are able to call tweens asynchronously. Also, GetChildren returns an array, so use ipairs instead.

3 Likes

Well, um. You could put them all in a transparent frame and then use Tween function on the Frame itself.

That wouldn’t work, If you change the Parent backgroundtransparency of a frame, but its the parent, it still wouldn’t change the descendants backgroundtransparency

;-; What should I do? Use spawn()???

1 Like

Given that your aim here is to tween over multiple Gui elements at one time, you will be required to iterate over the set of elements you’re looking to transition, create an individual tween for it and then run it, like igna’s code sample shows.

TweenService is only intended to facilitate transitions on a single object. You can create a function that hides away the fact that you’re doing this but ultimately there’s no point in doing that. There’s nothing wrong with creating multiple tweens so I’m not sure why you wouldn’t want to unless you’re concerned about readability which will not be affected in this scenario.

There’s one technical exception I can think of where you can tween multiple objects at once and that’s if you want to tween a model. I wrote a tutorial that covers smartly using welds in order to effectively tween multiple parts when you actually only tween model. If you’re interested.

There’s a way you could sync up all other TextLabels to a central value, tween that value and then set the TextTransparency of the rest of the labels, but I wouldn’t recommend it. I don’t believe Changed will fire fast enough to catch all intermediate frames of the tween.

1 Like

It works with spawn(), thank you @ignacasas06!

Do not use spawn!

You should remove the spawn and it’ll still work fine…

1 Like

Oh hey, could you easily explain different between ipairs and pairs? I never got it right, i’ve seen some tutorials, the most are in english and I speak spanish so kinda difficult? can you just really easy explain difference between ipairs and pairs? thanks so

ipairs counts up from 1…2…3…4…

pairs is random order

I speak spanish too, lol.

TAGS: Heart button not working…

There’s a few members in the community who write frequent posts about the difference so at the very least I’d be able to summarise what the deal is for you.

Essentially, you have what we call an array and a dictionary for tables.

local array = {1, 2, 3}

local dictionary = {
    ["A"] = true,
    ["B"] = true,
}

pairs:

  • Meant to go through tables where you set both a key and a value
  • Traversal (going through the table) does not have an order

ipairs:

  • Meant to go only through tables where you have elements
  • Traversal is guaranteed to be in order

ipairs is faster than pairs because the generator knows what the next key is and what order to run through the table in. Their use has also sometimes been the defining difference between whether something works properly or not. Use of ipairs also helps tell exactly what you’re iterating over.

Recent explanation I’ve given to another user (uses some terms you may be unfamiliar with): Script error table

1 Like

So for example

local hello = {"hello", "hi", "bye", "ok"}

for i,v in ipairs(hello) do
print(v)
end)
for i,v in pairs(hello) do
warn(v)
end)

this might be really derp but, I don’t find the difference?