Would a Loop do this?

Hello there, I know this may seem as a dumb question but I’d like to know how a loop loops through objects instead of tables.

Basically, Say there is a Parent and 3 children.

Here is a example:

for i = 1,4 do
   local UserId = game.Players[i] -- i will be a child and for every time it loops the child changes
end

If you didn’t understand that, Here’s a better example:
1 BOBUX

It loops through the first child, does something to it then goes back. Then it loops through 2’nd child, does something to it and goes back. So on.

I’d like to know how I could write a piece of code that’ll do exactly that, Please ask questions if your still unsure on what I mean.

Any help is appreciated, Thanks!

1 Like

I’m not sure if I understood it the right way. But I think using coroutines might help you. coroutine | Roblox Creator Documentation
coroutine | Roblox Creator Documentation

for Index, Value in pairs([locationsToValues]:GetChildren()) --Locations to the ValObjects. Make sure to use :GetChildren()
   if not Value:IsA("StringValue") then continue end
   -- Do what ever you need to with the values
end

Make sure to but :GetChildren() after you have defined the location. You could also use ipairs

1 Like

But doesn’t this loop through all of the children?

I meant that it loops through 1 child at a time:

Why would you need that exactly? Since if you are going to make changes separately at different times couldn’t you just define it yourself like this: local Value1 = [LocationOfValues].Value1. Or if you want to make different changes to each value then you can do something like this:

for Index, Value in pairs([locationsToValues]:GetChildren()) --Locations to the ValObjects. Make sure to use :GetChildren()
   if not Value:IsA("StringValue") then continue end
   if Value.Name == "Value1" then
      -- Make the changes to Value1
   elseif Value.Name == "Value2" then
      -- Make the changes to Value2
   end
   -- Continue
end

Loops already loop through each child in this case, so it doesn’t need to get Value1 then go back then Value2 and go back, this would a very inefficient way.

1 BOBUX

I’d like to so it loops through the child once at a time doing:

game.Players[Value.Value]

Once It gets a child, It sets a image to their HeadShot using :GetUserThumbnailAsync, That’s why I get 1 child at a time to fill in 1 image then the 2’nd child to fill in the 2’nd image.

I dont really know what your trying to do here, but you can use

game.Workspace.Values:GetChildren()

in the for loop (put it inside the pairs)

for i, v in pairs(game.Workspace.Values:GetChildren()) do

So what you want is to have it loop through all the player, and get there HeadShot thumbnail?

for _, player in pairs(game:GetService("Players"):GetPlayers()) do
   -- Settings for the Thumbnail
   local userId = player.UserId
   local thumbType = Enum.ThumbnailType.HeadShot
   local thumbSize = Enum.ThumbnailSize.Size420x420
   local content, isReady = Players:GetUserThumbnailAsync(userId, thumbType, thumbSize)
   -- ImageLabel.Image = content 
end

What this does is loop through each player and get there Headshot thumbnail, of course you have to define where you want it to go. So I know what your trying to say but the way you are imagining it is not how loops work. So what happens in a loop, what ever your looping so in this case the players it will iterate through each player and since each player is different it will always be a different Headshot Thumbnail, I think you are getting confused because you think it will make the same changes to each player, but that is only the case when you are working with constants, so things that will remain the same throughout. In this case the ThumbnailType and ThumbnailSize.

I hope this helps. I would recommend learning all the types of loops. Have a good day! :smiley:

1 Like

Oh wow, Thanks!

I thought those types of loops looped through all the players at once not indivigualy. To get a better understanding and for assurance this loops through each player in the service players one at a time?

Yeah there are many type of loops you can do, like earlier you did for i = 1, 4 do which would work if you did game:GetService("Players")[i] but there is another problem it will stop after 4 so what you could do is

local Players = game:GetService("Players")
for i = 1, #Players:GetPlayers() do -- '#' gets the length, so if there were 2 players the length would be 2
   local UserId = Players[i].UserId
   -- So on
end
1 Like