New: Nitro Boost our Discord server and receive full donation perks here on the website! Join the Scripting Helpers Discord Server to learn more! You can also Support on Patreon as always.
Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

Choosing a value using multiple tables unsolved

Asked by
TomsGames 205 Moderation Voter
5 years ago

Hi!

I've got a new system to pick a car in my game, and it uses some tables. Here are the tables I have:

creators = {
            "roblox",
            "rauluss1",
            "others"
}

carsroblox = {"ModBlueCar",
    "ModGreenJeep",
    "ModPoliceCar",
    "ModRedTruck",
    "ModSchoolBus",
    "ModSportsCar"}

carsrauluss1 = {"ModJumper",
     "ModGrandpaCar",
     "ModTaxi",
     "ModTraveller",
     "ModPartyCar",
     "ModRetro",
     "ModMonster",
     "ModBeetle",
     "ModKart"
}

carsothers = {"ModQuantum",
    "ModDarkHorse",
    "ModDominaytor",
    "ModSpeedaru"
}

Those are all my tables. Right, so I want to pick a car out of here with 2 variables set with some nice scripts.

The 2 numbers are defined as these:

table = number1.Value
car = number2.Value

So to pick the car what I am CURRENTLY doing is this:

chosentable = "cars"..creators[table]
chosencar = chosentable[car]

So in one line that is basically this:

"cars"..creators[table][car]

This returns the following error:

attempt to concatenate field '?' (a nil value)

What am I doing wrong?

1 answer

Log in to vote
0
Answered by
AxeOfMen 433 Moderation Voter
5 years ago

You are generating a string where you want a table.
This code

chosentable = "cars"..creators[table]

returns a string like "carsrauluss1". Note that this string does not represent your table named carsrauluss1. It is just a string. So, in effect, what you are attempting here is something like this:

"carsrauluss1"[3] --attempt to index a string. This returns nil.

An alternative approach is this

local cars = {
    {
        "ModBlueCar",
        "ModGreenJeep",
        "ModPoliceCar",
        "ModRedTruck",
        "ModSchoolBus",
        "ModSportsCar"
    },
    {
        "ModJumper",
        "ModGrandpaCar",
        "ModTaxi",
        "ModTraveller",
        "ModPartyCar",
        "ModRetro",
        "ModMonster",
        "ModBeetle",
        "ModKart"
    },
    {
        "ModQuantum",
        "ModDarkHorse",
        "ModDominaytor",
        "ModSpeedaru"
    },
}

local table = number1.Value
local car = number2.Value
chosencar  = cars[table][car]
Ad

Answer this question