Help Adding More Keys to a DataStore

I have this datastore script that works, but I can only get it to work with one key. I don’t know how to use multiple keys.

Script:

local DataStoreService = game:GetService("DataStoreService")
local SaveCash = DataStoreService:GetDataStore("SaveMoney")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Parent = player
	leaderstats.Name = "leaderstats"

	local Cash = Instance.new("IntValue")
	Cash.Parent = leaderstats
	Cash.Name = "Cash"
	
	local LoadCash
	local success,failure = pcall(function()
		LoadCash = SaveCash:GetAsync(player.UserId.."-Cash")
	end)
	if success then
		if LoadCash == nil then
			Cash.Value = 25000
		else
			Cash.Value = LoadCash
		end
		print("Loaded "..player.Name)
	else
		print("Failed to load "..player.Name)
		warn(failure)
	end
end)

local function SaveData(player)
	local success, failure = pcall(function()
		SaveCash:SetAsync(player.UserId.."-Cash", player.leaderstats.Cash.Value)
	end)
	if success then
		print("Saved "..player.Name)
	else
		print("Failed to save "..player.Name)
		warn(failure)
	end
end

local gameClosing = false

game.Players.PlayerRemoving:Connect(function(player)
	wait()
	if not gameClosing then
		SaveData(player)
	end
end)

game:BindToClose(function()
	gameClosing = true
	for _,player in pairs(game.Players:GetChildren()) do
		SaveData(player)
	end
end)

What do you mean to add more keys?

1 Like

So, I can already save the player’s money. I want to save more stuff like the player’s name and other stuff.

Ah yes! What you can do is just simply create a table (Preferably a dictionary) to store all of the Data you want to save!

local Data = {
    Cash = player.leaderstats.Cash.Value;
    OtherStat = player.leaderstats.OtherStat.Value;
}

When you call your SetAsync function when a Player leaves the game, you can save that table of data instead of having to use player.leaderstats.Cash.Value only!

And calling your GetAsync function when a Player joins back, you can make the LoadData variable equal to:

Cash.Value = LoadCash.Cash
OtherStat.Value = LoadCash.OtherStat

Since we’re wanting to get the key inside a dictionary to get it’s value :thinking:

1 Like

I’m not quite sure where to put the table.

Just put it inside where you call your SaveData function

local function SaveData(player)
    local DataToSave = {
        Cash = player.leaderstats.Cash.Value;
        OtherStat = player.leaderstats.OtherStat.Value; --This needs to be a valid stat, you can change this to whatever you want as long as you're able to obtain this
    }
	local success, failure = pcall(function()
		SaveCash:SetAsync(player.UserId.."-Cash", DataToSave)
	end)
	if success then
		print("Saved "..player.Name)
	else
		print("Failed to save "..player.Name)
		warn(failure)
	end
end

And when you retrieve it using your GetAsync function, do this:

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Parent = player
	leaderstats.Name = "leaderstats"

	local Cash = Instance.new("IntValue")
	Cash.Parent = leaderstats
	Cash.Name = "Cash"

    local RandomStat = Instance.new("IntValue")
    RandomStat.Name = "OtherStat"
    RandomStat.Parent = leaderstats
	
	local LoadCash
	local success,failure = pcall(function()
		LoadCash = SaveCash:GetAsync(player.UserId.."-Cash")
	end)
	if success then
		if LoadCash ~= nil then
			Cash.Value = LoadCash.Cash
            RandomStat.Value = LoadCash.OtherStat
		else
			Cash.Value = LoadCash
		end
		print("Loaded "..player.Name)
	else
		print("Failed to load "..player.Name)
		warn(failure)
	end
end)

The data that we saved inside the PlayerRemoving event will be a table, and we can load that table back when the player rejoins using GetAsync & setting its value to our loaded table stats

1 Like