Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do i iterate over every value of a table?

I have some departure boards in folders. Image of my departure board and GUI

Basically, It takes user input of the required properties and updates the departure board. My script to update 1 departure board works fine but I would like to iterate over every departure board inside of a folder, and change text labels inside of them. Here is my current code:

allDepartures = {}
updateDepartures.OnServerEvent:Connect(function(player, departure)
	allDepartures[#allDepartures + 1] = departure    
	local departureBoards = game.Workspace.departureBoards:GetChildren()
	print(departureBoards)
	for i, v in pairs(departureBoards) do
		print(i, v)
		for i = 1, 6, 1 do
			local folder = v.SurfaceGui:FindFirstChild(i)
			folder.Gate.Text = allDepartures[i][1]
			folder.Status.Text = allDepartures[i][2]
			folder.FlightNumber.Text = allDepartures[i][3]
			folder.AorB.Text = allDepartures[i][4]
		end
	end
end)

The non-functional part of this is the for loop. when printing my departureBoards table it shows all contents of my departureBoards folder. However, when attempting to use this to iterate over each of my departureBoards, it the print statement inside of my for loop only prints the first item of my table. How would i get this to iterate over each of my departure boards, or is there a better way to do what i'm trying to do? Any help would be greatly appreciated. Thanks!

2 answers

✓ Accepted answer
for _, DepartureBoard in ipairs(DepartureBoards:GetChildren()) do
	---------------
	local BoardComponents = DepartureBoard.SurfaceGui:GetChildren()
	---------------
	for InformationIndex, BoardComponent in ipairs(BoardComponents) do
		---------------
		BoardComponent.Text = (Departure[InformationIndex] or "[N/A]")
	end
end

2 comments

0

Give that a go:P

0

Thank you so much. after some minor tweaking this updated everything :D

allDepartures = {}
updateDepartures.OnServerEvent:Connect(function(player, departure)
	allDepartures[#allDepartures + 1] = departure
	for _, departureBoard in ipairs(game.Workspace.departureBoards:GetChildren()) do
		print(departureBoard)
		for i = 1, 6, 1 do
			local folder = departureBoard.SurfaceGui:findFirstChild(i)
			if allDepartures[i] ~= nil then
				folder.Gate.Text = allDepartures[i][1] 
				folder.Status.Text = allDepartures[i][2] 
				folder.FlightNumber.Text = allDepartures[i][3]
				folder.AorB.Text = allDepartures[i][4]
			end
		end
	end
end)

After copying and pasting Ziffixture's answer and tweaking it, the for loop worked fine and printed all of the departure boards. However, when adding my for loop it still didn't update all of the departure boards. I figured out that this was because I forgot to add some nil handling. After adding nil handling with a simple if statement, everything worked fine.

This discussion is preserved as it was. New answers, comments, and votes are closed.