Adding array to datastore?

Hi guys, I am making a datastore system using datastore 2 module. The game I’m working on has a car system and I need to save the list of cars bought, anyone know how to add it to my script?

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

local DataStore2 = require(script:WaitForChild("DataStore2"))

local DataVars = {
	["Cash"] = 100,
	["Hunger"] = 100,
	["Level"] = 1,
	["XP"] = 0

						}


function addPlayer(player)
	local DataFolder = Instance.new("Folder",player)
	DataFolder.Name = "Data"
	
	--Loop to make rep amounts
	for key, defaultval in pairs(DataVars) do
	local keystore = DataStore2(key, player)
    local RepAmt = Instance.new("NumberValue",DataFolder)
	RepAmt.Name = key
	
	local function UpdateRepAmt(RepAmt)
	RepAmt.Value = keystore:Get(defaultval)
	end
	
	UpdateRepAmt(RepAmt)
	keystore:OnUpdate(UpdateRepAmt(RepAmt))
	print(key.." loaded")
	end

	
end



for _,player in pairs(Players:GetPlayers())do
    addPlayer(player)
end

Players.PlayerAdded:connect(addPlayer)

DataStore2 allows you to save Dictionaries just like any other data type for a Player.

local DefaultData = {
    ["Coins"] = 0;
    ["XP"] = 0;
    ["Stats"] = {
        ["Kills"] = 0;
    }
}

-- :Get()

local Data = DataStore2(DataStoreName, Player):Get(DefaultData)

-- Modifying

Data["Coins"] = 10

-- :Set()

DataStore2(DataStoreName, Player):Set(Data)

On a side note - in the future please use the search feature to see if what you are about to post already exists.

3 Likes