How to save multiple values in one data store

Hello Everybody, I have a data store that saves the amount of Coins players, but I need it too save, gems too

server script

local DataStoreService = game:GetService("DataStoreService")

local myDataStore = DataStoreService:GetDataStore("myDataStore")

game.Players.PlayerAdded:Connect(function(plr)
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = plr
	
	local Coins = Instance.new("IntValue")
	Coins.Name = "Coins"
	Coins.Parent = leaderstats
	Coins.Value = 250
	
	local Gems = Instance.new("IntValue")
	Gems.Name = "Gems"
	Gems.Parent = leaderstats
	

	
	--if plr.Name == "mariyandog999" or plr.Name == "deadpoolmonkey67"  then 
		
		--plr.leaderstats.Coins.Value = 10000
		--plr.leaderstats.Gems.Value = 10000
	--end
	local data 
	local succcess, errormessage = pcall(function()
		data = myDataStore:GetAsync(plr.UserId.."-Coins")
		
	end)
	
	if succcess then 
		Coins.Value = data
	else
		print("Error getting data")
		warn(errormessage)
	end
	
	
end)

game.Players.PlayerRemoving:Connect(function(plr)
	
	local success, errormessage = pcall(function()
		myDataStore:SetAsync(plr.UserId.."-Coins",plr.leaderstats.Coins.Value)
		
	end)
	
	if success then 
		print("Player data was successfuly saved")
		
	else 
		print("Error saving data")
		warn(errormessage)
	end
end)
	

I tried convert data into a table but I can’t quiet get it to work. I think if set the data variable to something else it might work.

1 Like

Saving a table is how you would go about saving multiple values, could you show me how you saved the table?

Maybe these could help?

This is what I did

local DataStoreService = game:GetService("DataStoreService")

local myDataStore = DataStoreService:GetDataStore("myDataStore")

game.Players.PlayerAdded:Connect(function(plr)
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = plr
	
	local Coins = Instance.new("IntValue")
	Coins.Name = "Coins"
	Coins.Parent = leaderstats
	Coins.Value = 250
	
	local Gems = Instance.new("IntValue")
	Gems.Name = "Gems"
	Gems.Parent = leaderstats
	

	
	--if plr.Name == "mariyandog999" or plr.Name == "deadpoolmonkey67"  then 
		
		--plr.leaderstats.Coins.Value = 10000
		--plr.leaderstats.Gems.Value = 10000
	--end
	local data = {Coins.Value;
		Gems.Value
	}
	
	local succcess, errormessage = pcall(function()
		data = myDataStore:GetAsync(plr.UserId.."-Coins",plr.UserId.."-Gems")
		
		
		
	end)
	
	if succcess then 
		Coins.Value = data
	else
		print("Error getting data")
		warn(errormessage)
	end
	
	
end)

game.Players.PlayerRemoving:Connect(function(plr)
	
	local success, errormessage = pcall(function()
		myDataStore:SetAsync(plr.UserId.."-Coins",plr.leaderstats.Coins.Value,   plr.UserId.."-Gems",plr.leaderstats.Gems.Value)
		
	end)
	
	if success then 
		print("Player data was successfuly saved")
		
	else 
		print("Error saving data")
		warn(errormessage)
	end
end)
	

output:Unable to cast value to Object - Server

Do you think I should open a second data store just for the second value or will that create lag?

Ok, I made a lot of modifications, tell me if this works. If you need me to explain anything, please just let me know!

local Players = game:GetService('Players')

local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")


local function OnPlayerAdded(player)
	
	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"

	local Coins = Instance.new("IntValue", leaderstats)
	Coins.Name = "Coins"
	Coins.Value = 250

	local Gems = Instance.new("IntValue", leaderstats)
	Gems.Name = "Gems"
	
	
	local data = nil

	local success, errormessage = pcall(function()
		data = myDataStore:GetAsync("Player_"..player.UserId)
	end)

	if success and data ~= nil then 
		print("Player data loaded successfully!")
		
		Coins.Value = data['Coins']
		Gems.Value = data['Gems']
		
	elseif not success then
		
		print("Error getting player data!")
		warn(errormessage)
	else
		print(player.Name..' is a new player.')
	end
end

local function CreateTable(player)

	local PlayerData = {}

	for _, Value in pairs(player.leaderstats:GetChildren()) do

		PlayerData[Value.Name] = Value.Value
	end

	return PlayerData
end

local function OnPlayerRemoving(player)


	local PlayerData = CreateTable(player)
	local success, errormessage = pcall(function()

		myDataStore:SetAsync("Player_"..player.UserId, PlayerData) 
	end)
	
	if success then
		print("Player data was successfuly saved!")
	else
		print("Error saving data!")
		warn(errormessage)
	end
end

Players.PlayerAdded:Connect(OnPlayerAdded)
Players.PlayerRemoving:Connect(OnPlayerRemoving)

game:BindToClose(function()
	for i, player in pairs(Players:GetChildren()) do
		OnPlayerRemoving(player)
	end
end)
4 Likes

It does not work. The will not save, the coins and gems

1 Like
local dss = game:GetService("DataStoreService"):GetDataStore("Yes")
function savedata(plr)
   local coins = plr.leaderstats.Coins.Value
   local gems = plr.leaderstats.Gems.Value

   dss:SetAsync(plr.UserId.."-data", {coins, gems})
end

game.Players.PlayerAdded:Connect(function(plr)
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = plr
	
	local Coins = Instance.new("IntValue")
	Coins.Name = "Coins"
	Coins.Parent = leaderstats
	Coins.Value = 250
	
	local Gems = Instance.new("IntValue")
	Gems.Name = "Gems"
	Gems.Parent = leaderstats
	
	local data 
	local success, errormessage = pcall(function()
		data = myDataStore:GetAsync(plr.UserId.."-Coins")
		
	end)
	
	if succcess then 
		Coins.Value = data[1]
                Gems.Value = data[2]
	else
		print("Error getting data")
		warn(errormessage)
	end
end)

game.Players.PlayerRemoving:Connect(savedata)

game:BindToClose:Connect(function() -- When server is gonna shut down
   for i, plr in game.Players:GetPlayers() do
      savedata(plr)
   end
end)
local DSS = game:GetService("DataStoreService")
local GameData = DSS:GetDataStore("Yes")

local Players = game:GetService("Players")

local function loadData(Player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	
	local Coins = Instance.new("NumberValue")
	Coins.Name = "Coins"
	Coins.Value = 250
	Coins.Parent = leaderstats
	
	local Gems = Instance.new("NumberValue")
	Gems.Name = "Gems"
	Gems.Parent = leaderstats
	
	local playerDataKey = Player.UserId .. ".data001"
	local playerData
	local dataLoaded, loadError = pcall(function()
		playerData = GameData:GetAsync(playerDataKey)
	end)
	
	if not dataLoaded then warn("Error loading data\n"..loadError) end
	if dataLoaded and playerData then
		Coins.Value = playerData.Coins or Coins.Value
		Gems.Value = playerData.Gems or Gems.Value
	end
end

local function saveData(Player)
	local leaderstats = Player:FindFirstChild("leaderstats")
	if leaderstats then
		local dataTable = {}
		dataTable.Coins = leaderstats.Coins.Value
		dataTable.Gems = leaderstats.Gems.Value
		local playerDataKey = Player.UserId .. ".data001"
		pcall(GameData.GetAsync, GameData, playerDataKey, dataTable)
	end
end

Players.PlayerAdded:Connect(loadData)
Players.PlayerRemoving:Connect(saveData)

game:BindToClose(function()
	for _, Player in pairs(Players:GetPlayers()) do
		saveData(Player)
	end
	task.wait(5)
end)

I just tested it and it works perfectly fine, did you change something?

Nvm I joined the game and it works, it just did not work in studio for some reason. TYSM :slight_smile:

1 Like

Sorry for bumping up this topic, but that is because you forgot to turn on API services access in studio.

To turn this on you go to:
Settings → Security → Enable Studio Access to API Services