How to find Table

local trainSpawnsAndLines = {["9"] = {"Ostheim"}}

I think i can find “Ostheim” with if table.find(trainSpawnsAndLines, tostring(stationName)) but how can i find the Number? if table.find(trainSpawnsAndLines, tostring(stationName)[tostring(selectedLine)]) then but that dont works.

Edit: Would this work?:

if trainSpawnsAndLines[stationName][selectedLine] then

1 Like

table.find does not work on dictionaries. Documentations say it works on array-like tables. Just check directly if trainSpawnsAndLines[stationName] ~= nil and then you’re good from there.

Assume and let trainSpawnsAndLines be a table that contains an arbitrary amount of numbers as strings:

{"2", "5", "9", "474"}

As you can see, it is array-like. To access the number, simply go for:
table.find(trainSpawnsAndLines[stationName], selectedLine)

That gives me a Error

Why don’t you just do 'local Stations = {

[9] = ‘Ostheim’,
}’

Then do table .find(Stations,‘Ostheim’)
that should return the table index where ‘Ostheim’ is in so 9

How could i print the 9 then


Do it as a variable so where the table.find is make it
local TableIndex = table.find(Stations,‘Ostheim’)

Print(TableIndex)

local trainSpawnsAndLines = {["9"] = "Ostheim"}

local trainSpawnsAndLinesVariable = table.find(trainSpawnsAndLines, tostring(stationName))

if trainSpawnsAndLinesVariable then

Didnt worked

remove the speech marks around the

The “” around 9?


Yes sorry forgot to add that bit in

I did it didnt worked


Hmm this usually work for me whenever i trying to find a value like this.

This worked for me

1 Like

You can try adding the station name as a key and keep a set of lines as the value:

local trainSpawnsAndLines = {
    ["Ostheim"] = {
        ["9"] = true
    }
}

you can then check for a certain station with a certain line by doing this:

local trainLines = trainSpawnsAndLines[tostring(stationName)]

if trainLines and trainLines[tostring(selectedLine)] then
    print("found line")
end

You can add another line to the station by writing in something like ["#"] = true,

Edit: if you know ahead of time that the station name will always be a string, you can just omit the tostring call for the trainLines statement.

local trainLines = trainSpawnsAndLines[stationName]

You can do a similar thing for selectedLine if you know it will always be a string.

1 Like

But you dont have the Line added in your script

Which line are you talking about

What do you mean here? table.find() returns the index (number) of the object.

table.find(trainSpawnsAndLines, "Ostheim") -- returns the index

I need to check if 9 Exist and if Ostheim is inside 9

Then put 9 stops before that maybe?

Also dont worked