How to find Table

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


Is it not printing at all? Are there any errors? Do the stationName and selectedLine strings contain any hidden characters or are they misspelled?

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

if trainSpawnsAndLines[9] ~= nil and table.find(trainSpawnsAndLines[9], "Ostheim") ~= nil then
	-- do stuff
end

It worked, Thanks!


1 Like
 ~= nil

This isn’t necessary by the way.

1 Like