Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 6 years ago

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

1local stations = player.PlayerGui.SwitchGui.Stopping_Pattern.StationsList:GetChildren()
2local switchesgui = player.PlayerGui:WaitForChild("SwitchGui")
3local stoppingPatternFolder = switchesgui:WaitForChild("Stopping_Pattern")
4local nextstation = stoppingPatternFolder:WaitForChild("NextStop")
5local nextstation_text = nextstation.Value
6local station = game.Workspace:WaitForChild(nextstation_text)
7for i = 1, #stations do
8    station.P1.Screen1.Scroller1.Label.Text = stations[i]
9end

I know this isn't how you do it because it only displays the last station on the display instead of them all.

0
Im not sure i understand the issue. I can list 6 of them in order. casper123123123 357 — 6y
0
Add me on discord and I might be able to help more. Casper#0357 casper123123123 357 — 6y
0
the issue is that I don't know how to script it lol kieranhendy 28 — 6y
0
I've added you on discord I'll try explain the issue better there kieranhendy 28 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago

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.

01function StationsToString(stations)
02    local s = {} -- becomes the string to return
03    for i = 1, #stations do
04        s[#s+1] = stations[i].Name -- or .Value or whatever
05        if i < #stations - 1 then -- a comma after everything except the last and 2nd last values
06            s[#s+1] = ", "
07        elseif i == #stations - 1 then -- right before the last item
08            s[#s+1] = " & "
09        end
10    end
11    return table.concat(s)
12end
13station.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.

Ad

Answer this question