Adding New Vehicles to a Datastore (Inventory)

I’m creating a buyable car script that saves bought cars, but I wanted to know, assuming I store the bought cars in a dictionary for the datastore: how do I add new vehicles to the datastore so that they add to all players’ saved data?

You could just save the cars that the player actually owns, and compare that to a datastore that has a list of all the cars available.

1 Like

But would that automatically add the new car (s) to everyone’s data with a false value, and if so, how would I do that?

I manage my inventory datastores by a dictionary like so,

local data = {}

data.Car = {}
data.Planes.ExampleCar = true

data.Planes = {}
data.Planes.ExamplePlane = true

data.Stats = {}
data.Stats.Coins = 0
data.Stats.Level = 1
data.Stats.Exp = 49

-- save data

so in your case, you can do something like so,

local data = {}
data.Cars = {}

local ownedcars = data.Cars
ownedcars.Ford = true
ownedcars.ToyCar = true

-- save data

--------------------------------------
--------------------------------------

-- To add new cars just get the previous data and do something like so,

local ownedcars = prevdata.Cars
ownedcars.NewCarName = true

-- save data
1 Like

See this

1 Like

For my solution, I need to check if a player owns vehicle x, but not vehicle y.
To do that with an array, I’d have to loop through the values of the array to check if they own it. To do so with a dictionary, I would just have to check if the key is equal to anything other than nil.
If that’s correct, wouldn’t it be more efficient to use a dictionary?

If you use a dictionary you can set an index with the name of the vehicle to true to mark it as owned. In order to check if they own it, you can simply index the name of the vehicle, if it’s true, then they own the vehicle, otherwise they don’t own it. It’s much faster to check than iterating through a table of strings with the vehicle names. However, you will have to iterate through the dictionary if you need to check the total number of vehicles they own.

1 Like

I don’t think I’ll have a use case for needing to check the number of cars owned. Thank you so much!