Creating tweens for multiple Gui elements

No, same position.

TAGS: Heart button not working…

1 Like

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

2 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
4 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 - #5 by colbert2677

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?

if you put:

hello[6] = 'jdkh'
hello[5] = 'nxzvzk'

after defining the table, you’ll see the difference.

The problem is, never rely on this behavior. Always use ipairs for lists. If you make a dictionary (list and dictionary are both tables in lua), where for example you go like hello.yes = 5, you’ll need pairs because ipairs won’t pick up the yes.

1 Like
u={}
u[1]="a"
u[3]="b"
u[2]="c"
u[4]="d"
u["hello"]="world"

for k,v in pairs(u) do print(k,v) end
for k,v in ipairs(u) do print(k,v) end

Expected output:
pairs

1 a
2 b
3 c
4 d
hello world

ipairs

1 a
2 b
3 c
4 d

Basically for tables / arrays use ipairs. (Where the index will always be numerical)
And for dictionaries use pairs. (Mix of numberical indexes and / or words)