Looping function in gui script only finding one ascendant for v

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve?
    I’m currently challenging my basic programming skills with gui interface. In short, I ran in to a problem with pairs of i,v and need some help with the error I made that I can’t seem to debug.

  2. What is the issue?
    The function “BoxDisplay” seems to not recognize the textbuttons that have been looped on being found in the function, as it prints nil when I put v in a table to debug and only prints out “Exit” when I print out “Table[1]” which is an image button, when I try to find the rest such as "Table[2], it prints out nil.

  3. What solutions have you tried so far?
    Currently I could not find a fix to this problem, from printing, I can only assume that It’s trying to find the buttons while it hasn’t loaded, but the variable of MainPanel already has the WaitForChild trigger, so it should’ve rendered the buttons.

-- Don't mind the names of the variables, I just like taking it seriously, also I haven't done scripting for awhile now so do forigve me if this was a simple fix all along
Panel = script.Parent

Players = game:GetService("Players")

ReplicatedStorage = game:GetService("ReplicatedStorage")

MainPanel = Panel:WaitForChild("Main GUI")

PlayerList = MainPanel:FindFirstChild("PlayerList")

Staff = Players.LocalPlayer

PlayerBar = ReplicatedStorage:WaitForChild("TextButton")

RandomWeapons = ReplicatedStorage:WaitForChild("RandomWeapons")

function StartUp()
	MainPanel:FindFirstChild("Developer Panel.").Text = Staff.Name .. ": Administrator Panel"
end

function System()
	for i,v in pairs(Players:GetChildren()) do
		local CloneHolder = PlayerBar:Clone()
		CloneHolder.Parent = PlayerList
		CloneHolder.Name = v.Name
		CloneHolder.Text = v.Name
	end
end

function BoxDisplay()
	for i,v in pairs(MainPanel:GetChildren()) do
		local Table = {v}
		print(Table[2])
		v:FindFirstChildOfClass("TextButton").MouseButton1Click:Connect(function()
			if v.Name == "Placeholder" then
				print("Worked!")

			elseif v.Name == "Menu" then 
				print(v.Name, "Worked!")
				
			elseif v.Name == "Testing" then
				print(v.Name, "Worked!")

			elseif v.Name == "Moderation" then
				print(v.Name, "Worked!")

			elseif v.Name == "Development" then
				print(v.Name, "Worked!")
			
			end
		end)
	end
end

System()
StartUp()
BoxDisplay()

Table is an array with only 1 value (which is v). Therefore, if you try to index the second value with Table[2], it will be nil because there’s only 1 value

“pairs of i,v” is not a feature of Luau. This is a common misunderstanding with beginners. The loop you’re referring to is formally known as a “generic for loop”. This loop utilizes iterator functions, which are functions that describe how something is traversed and what attributes it yields during traversal.

pairs is a built-in iterator factory which produces an iterator function tailored to your table. It returns the index and value of the current element it’s traversing over. These values are stored in your loop variables, which the majority have poorly standardized as “i”, meaning index, and “v”, meaning value. These loop variables can be named anything, and I encourage you to give them a more concise name.

Investigating the value of “v” in the current iteration shows it’s only one value:

for index, value in pairs({"A", "B", "C"}) do
    print(value)
end

Output:

A
B
C

You’re storing this value within a table, so naturally, that table will only contain one value. This is why you’re seeing no second index

1 Like

Ah, so I’ve been looping them wrong and have been learning pairs wrong aswell, I can see what I need to change now, thank you so much.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.