Small question about tables and data saving that i can't exactly shorten

hello all! i’ve been having a little mental issue with not being able to realize how i can do a thing.

i’m making a car game, and each car is going to have specific data that gets saved into a datastore, my issue, i don’t know how to make each car have its own individual set of saved values.

So pretty much, Car 1 would have a set of individual values (value1 and value2), and Car 2 would have the same set of values, but they would be unique to Car 2, and would not be the same as Car 1, same goes the other way.

Does anyone know how i could do this? i am genuinely lost with what to do in this case and this is something i need solved to be able to advance.

many thanks in advance!

-- get your data per car
local car1 = {
    speed = 1,
    color = "red",
}

local car2 = {
    speed = 2,
    color = "yellow",
}

-- combine the tables into an array
local all_cars = {}
table.insert(all_cars, car1)
table.insert(all_cars, car2)

-- all_cars[1] == car1

-- save the combined table
datastore:SetAsync("ownedCars" .. player.UserId, all_cars)
2 Likes

and in order to get something like, the speed of one of car1, i’d just have to do something like this?

local car1 = all_cars[1]
local speed = car1[1]
1 Like

edit: @/Gert got you the correct solution

Almost correct! speed would still be a string property

local car1 = all_cars[1]
local speed = car1.speed
1 Like

ah, alright, thank you very much! hyper appreciated :slight_smile:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.