For I loop running twice

Hello there, I’ve been on this for a bit and am very confused about how this prints twice?

I have this script:

					for _, Player2 in pairs(Button.Owner:GetChildren()) do
								print("Done")
								local userId = Player.UserId
								local thumbType = Enum.ThumbnailType.HeadShot
								local thumbSize = Enum.ThumbnailSize.Size420x420
								local content, isReady = game.Players:GetUserThumbnailAsync(userId, thumbType, thumbSize)

								if OwnerClone.Image2.Image and OtherClone.Image2.Image == "rbxasset://textures/ui/GuiImagePlaceholder.png" then
									OwnerClone.Image2.Image = content
									OtherClone.Image2.Image = content 
									break
								else
								    print("Else statment")
								end

It prints done twice when Button.Owner only has 1 child which makes no sense.

I’m really confused and I’d like to know why this is happening, Any help is appreciated. Thanks !

Is there anything else in your code that might make it print ‘Done’ twice?

What happens when you try printing the “Player2” variable as well?

Nope, Only that prints done nothing else.

I’ll see and try that, Thanks.

It prints the Player2 variable twice too

1 Like

Try printing the table that the for loop is iterating over. I’d recommend moving it to a variable just before the loop:

local children = Button.Owner:GetChildren()
print(children)
for _, Player2 in pairs(children) do
--... [the rest of your code]

In the output you should be able to see a expandable table to see what it contains.

What’s the difference between moving it to a variable and not?

Well there seems to be some confusion about what the contents of the table is. I just suggested moving it outside to loop like that so that you could debug and possibly find out what’s causing the issue.

Does the children have children in it?
E.g
There is a textlabel in owner,but there is an uicorner inside textlabel.

Can we see more code? Maybe it’s inside of a function that gets called twice.

So basically, There is a TextLabel. Inside the TextLabel is something called Owner and then I use :GetChildren to get the children of the owner in the TextLabel.

Sorry if this doesn’t make sense, If so feel free to tell me what you don’t understand!

Its not a function, Its a OnServerEvent. And its only in the loop where it prints something x2 nowhere else

1 Like

If its the same output each time that suggests that the loop is being run multiple times from an enclosing scope so I’d need to see more code to trace down what’s happening.
If it’s linked to an event then that event is probably being fired multiple times.

Yeah, event. But the event is connected to a function (that’s where your for loop is)
Anyways can we see where you fire the event?

If you mean something like:

local function e (Thing)
     -- Do something
end)

Then no, I don’t have anything like that.

Here is my script:

						local OwnerClone = game.Players[Button.Owner.Value].PlayerGui.MenuUI.PlayerLobby
						OwnerClone.NameOfMatch.Text = NameText
					    OwnerClone.Visible = true 
						OwnerClone.OwnerLobby.Value = Button.Owner.Value

						local Clone = Instance.new("StringValue")
						Clone.Name = "Member"
						Clone.Value = Player.Name
						Clone.Parent = OwnerClone:WaitForChild("OwnerLobby") -- Value
						
					    local OtherClone = OwnerClone:Clone()
						OtherClone.NameOfMatch.Text = NameText
						OtherClone.Visible = true
						OtherClone.Parent = game.Players[Obj.Value].PlayerGui.MenuUI

						------- SOLO -------


						if Choose == "Solo(1v1)" then
							OwnerClone.Image3:Destroy()
							OwnerClone.Image4:Destroy()

							OtherClone.Image3:Destroy() -- Clone at the bottom clone 
							OtherClone.Image4:Destroy()

							for i = 1, 2 do
								local UserId = game.Players[Button.Owner.Value].UserId
								local thumbType = Enum.ThumbnailType.HeadShot
								local thumbSize = Enum.ThumbnailSize.Size420x420
								local content = game.Players:GetUserThumbnailAsync(UserId, thumbType, thumbSize)

								OwnerClone.Image1.Image = content
								OtherClone.Image1.Image = content

								local UserId2 = game.Players[Obj.Value].UserId
								local thumbType2 = Enum.ThumbnailType.HeadShot
								local thumbSize2 = Enum.ThumbnailSize.Size420x420
								local content2 = game.Players:GetUserThumbnailAsync(UserId2, thumbType2, thumbSize2)

								OwnerClone.Image2.Image = content2
								OtherClone.Image2.Image = content2
							end 
						end
						

						-------- TEAMS --------


						if Choose == "Teams(4v4)" then
              
							local UserId = game.Players[Button.Owner.Value].UserId
							local thumbType = Enum.ThumbnailType.HeadShot
							local thumbSize = Enum.ThumbnailSize.Size420x420
							local content = game.Players:GetUserThumbnailAsync(UserId, thumbType, thumbSize)

							OwnerClone.Image1.Image = content
							OtherClone.Image1.Image = content
							
							for _, PlayerE in pairs(Button.Owner:GetChildren()) do -- Loops twice

								local userId = Player.UserId
								local thumbType = Enum.ThumbnailType.HeadShot
								local thumbSize = Enum.ThumbnailSize.Size420x420
								local content, isReady = game.Players:GetUserThumbnailAsync(userId, thumbType, thumbSize)

								if OwnerClone.Image2.Image and OtherClone.Image2.Image == "rbxasset://textures/ui/GuiImagePlaceholder.png" then
									OwnerClone.Image2.Image = content
									OtherClone.Image2.Image = content
									break
								else
								    print("Else statment")
								end
								
								
								print("Here") 
								
								if OwnerClone.Image3.Image and OtherClone.Image3.Image == "rbxasset://textures/ui/GuiImagePlaceholder.png" then
									OwnerClone.Image3.Image = content
									OtherClone.Image3.Image = content
									break
								else
									print("Continuing")
								end

								if OwnerClone.Image4.Image and OtherClone.Image4.Image == "rbxasset://textures/ui/GuiImagePlaceholder.png" then
									OwnerClone.Image4.Image = content
									OtherClone.Image4.Image = content
								else
									break
								end
							end
						end

@McThor2

Then you probably have something like this:

Event.OnServerEvent:Connect(function() -- that's an anonymous *function*

end)

Let’s not get off-topic though.

Can I get a reply for this?

local debounce = false

script.Parent.MouseButton1Click:Connect(function()
	print("Player clicked lobby")
	if debounce == false then
		debounce = true
		game.ReplicatedStorage.MenuRemotes.JoinLobby:FireServer(script.Parent.Parent.TeamValue.Text, script.Parent.Parent, script.Parent.Parent.Text)
		wait(3)
		debounce = false
	end
end)