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)
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...
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.
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