Will this work to save multiple values to the datastore at once?

Not really familiar with Datastores, will this work to save and load multiple stats at once?
and if not what is another way I can use to do that?


local players = game:GetService("Players")
local DataStore = game:GetService("DataStoreService")
local GameData = DataStore:GetDataStore("GameData")

players.PlayerAdded:Connect(function(player)	
local Int = Instance.new("IntValue",player) Int.Name = ("leaderstats")
	local kos = Instance.new("NumberValue",Int) kos.Name = ("KOs")
		local points = Instance.new("NumberValue",Int) 
			points.Name = "Points"
			points.Value = 15000
				local vc = Instance.new("NumberValue",Int) vc.Name = ("VirtualCurrency")	
					local lvl = Instance.new("NumberValue",Int) lvl.Name = ("lvl")
						local xp = Instance.new("NumberValue",lvl) xp.Name = ("XP")
							local Swords = Instance.new("Folder",player) Swords.Name = "Swords"
	
	wait(1)
for a, b in ipairs(player.leaderstats:GetDescendants()) do			
local stuff = 	GameData:GetAsync(player.UserId.."-"..b.Name)			
		b.Value = stuff
		print("loading "..b.Name)
		end
end)


players.PlayerLeaving:Connect(function(player)
	for a, b in ipairs(player.leaderstats:GetDescendants()) do
		GameData:SetAsync(player.UserID.."-"..b.Name, b.Value)	
		print("Saving "..b.Name.." "..b.Value)
	end		
end)

bump. bump. bump. bump. bump. bump. bump.

Refrain from calling SetAsync() for each piece of data. Instead, package the data into a table for storage.

How would I implement the table into :SetAsync() ?

This should work.

local players = game:GetService("Players")
local DataStore = game:GetService("DataStoreService")
local GameData = DataStore:GetDataStore("GameData")

players.PlayerAdded:Connect(function(player)	
	local Int = Instance.new("IntValue",player) Int.Name = ("leaderstats")
	local kos = Instance.new("NumberValue",Int) kos.Name = ("KOs")
	local points = Instance.new("NumberValue",Int) 
	points.Name = "Points"
	points.Value = 15000
	local vc = Instance.new("NumberValue",Int) vc.Name = ("VirtualCurrency")	
	local lvl = Instance.new("NumberValue",Int) lvl.Name = ("lvl")
	local xp = Instance.new("NumberValue",lvl) xp.Name = ("XP")
	local Swords = Instance.new("Folder",player) Swords.Name = "Swords"

	wait(1)
	local success, err = pcall(function()
		local stuff = GameData:GetAsync(player.UserId.."-StatsData")
		for a, b in ipairs(stuff) do		
			for c, d in ipairs(player.leaderstats:GetDescendants()) do
				if d.Name == a then
					d.Value = b
				end
			end
		end
	end)
	if success then
		print("loaded data for "..player.Name)
	else
		print("loading data for "..player.Name.." failed.",err)
	end
end)


local function Save(player)
	local DataTable = {}
	for a, b in ipairs(player.leaderstats:GetDescendants()) do
		DataTable[b.Name] = b.Value
	end		
	local success, err = pcall(function()
		GameData:SetAsync(player.UserID.."-StatsData", DataTable)	
	end)
	if success then
		print("saved data for "..player.Name)
	else
		print("saving data for "..player.Name.." failed.",err)
	end
end

players.PlayerLeaving:Connect(function(player)
	Save(player)
end)

game:BindToClose(function()
	for i,player in pairs(game.Players:GetChildren()) do
		pcall(Save,player)
	end
end)
2 Likes

I would also recommend using the DataStore2 module for this use case. It handles everything for you :slight_smile:

1 Like

loading data for killaman333 failed. ServerScriptService.SaveScript:19: invalid argument #1 to ‘ipairs’ (table expected, got nil)

??

When the player’s data is retrieved. Check if the stuff variable exists or is nil.
The data will return nil if the player doesn’t have saved data.

2 Likes