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

Displaying a trains station stopping pattern using :GetChildren()?

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.

4 comments

0

Im not sure i understand the issue. I can list 6 of them in order.

0

Add me on discord and I might be able to help more. Casper#0357

0

the issue is that I don't know how to script it lol

0

I've added you on discord I'll try explain the issue better there

1 answer

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.

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