Hi there, basically im trying to get a platform display to show the stations that the train is stopping at in the correct order but im not sure how to use :GetChildren() and have it display in the correct order and also with commas in between. eg. it should say "Station1, Station2 & Station3" but atm the best i can figure out is getting it to display 1 of the stations. I was using :GetChildren() because I want it to work with different train services with different amounts of stations en-route so sometimes the display might say "Station1, Station2, Station3, Station4, Station5 & Station6" other times it might be more or less stations. Thanks for the help :)
local stations = player.PlayerGui.SwitchGui.Stopping_Pattern.StationsList:GetChildren() local switchesgui = player.PlayerGui:WaitForChild("SwitchGui") local stoppingPatternFolder = switchesgui:WaitForChild("Stopping_Pattern") local nextstation = stoppingPatternFolder:WaitForChild("NextStop") local nextstation_text = nextstation.Value local station = game.Workspace:WaitForChild(nextstation_text) for i = 1, #stations do station.P1.Screen1.Scroller1.Label.Text = stations[i] end
I know this isn't how you do it because it only displays the last station on the display instead of them all.
You just need a for loop with some if statements to handle the punctuation.
I'm going to use efficient tables for string concatenation. In the below code, s
is short for 'string' and is a table of strings to concatenate. s[#s+1] = ____
is how you add something to the table.
function StationsToString(stations) local s = {} -- becomes the string to return for i = 1, #stations do s[#s+1] = stations[i].Name -- or .Value or whatever if i < #stations - 1 then -- a comma after everything except the last and 2nd last values s[#s+1] = ", " elseif i == #stations - 1 then -- right before the last item s[#s+1] = " & " end end return table.concat(s) end station.P1.Screen1.Scroller1.Label.Text = StationsToString(stations)
Technically you don't need that code in a function, but it's easier to organize that way.