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

Why is this table giving different values?

Hi all. I have a table that contains all of the routes in my bus driving game. This problem has totally stumped me and as a fairly proficient programmer I have no idea what is going on. (something supernatural?!)

allroutes = {"1","2","7","10","11C","15","22","25","36","42","50","57","58","60","72","89","99","490","X9"}
	

	bus = workspace:WaitForChild(player.Name.."'s Car").BusValue.Value
	Gui = player.PlayerGui.GUI.Routes.Selector:GetChildren()
	
	
	for i,child in pairs(Gui) do
			print("#"..i.."Route: "..allroutes[i])
			child.Route.Value = allroutes[i]
	end

The print outputs each part of the table as so: (I've only included two) #1 Route: 1 #6 Route: 15

EXCEPT... and this is the astounding bit... the Value assignment is completely broken. Well sort. There are numbered imagebuttons from 1 to 19, and the code cycles through each and adds the next number in the table to the numbered imagebutton's value. So the last imagebutton is called "19" and should* have a value of "X9" but here is what actually happens...

#(1): 1 #(2): 2 #(3): 10 #etc... (as normal) #(6): 42 should be: 15 #(7): 60 should be: 22 #(8): 25 (correct) #(19): 89 should be: X9.

The assignments are all jumbled up and not going in the order that they appear in the table. Can anyone see the problem here? I am stumped on this one. Thanks.

1 answer

✓ Accepted answer

You're iterating through the children of the GUI. The children are not necessarily sorted in the order you decided they're supposed to be in, so you can't rely on that assumption.

Instead, list the GUI elements in a table that you order manually, and iterate through that table. Another thing you can do is name the GUI elements numerically in order and write

local Gui=player.PlayerGui.GUI.Routes.Selector
for i=1,#allroutes do
	print("#"..i.."Route: "..allroutes[i])
	Gui:FindFirstChild(i).Route.Value=allroutes[i]
end

1 comment

0

Aha! I completely glossed over the fact they might not be in the order I wanted them. They were numbered but hey-ho you're code worked. Thanks :D

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