Saving Tables in DataStore

I have a folder inside of player called PlayerData and a folder inside of that called Inventory, at the moment I’m saving the values inside of Inventory and loading them using a table. This works perfectly (Thanks to some help)

local DSService = game:GetService("DataStoreService")
local DS = DSService:GetDataStore("DataStore")
local players = game:GetService("Players")
local player = players.LocalPlayer or players.PlayerAdded:Wait()
local storage = player:WaitForChild("PlayerData"):FindFirstChild("Inventory")
local pdata = player:WaitForChild("PData")

players.PlayerAdded:Connect(function()
	local load = DS:GetAsync("User_"..player.UserId) --load data from datastore
	if type(load) == "table" then --expected type of value
		for i, v in pairs(load) do --cycle through dictionary of tables
			for name, value in pairs(v) do --cycle through those tables
				local intVal = Instance.new("IntValue")
				intVal.Name = name
				intVal.Value = value
				intVal.Parent = player:WaitForChild("PlayerData"):FindFirstChild("Inventory") --load all the intvalue instances back into storage
				--perform other code
			end
		end
	else
		load = {} --empty table indicates no intvalues
		--perform other code
	end
end)

players.PlayerRemoving:Connect(function(player)
	local data  = {}
	for i, v in pairs(storage:GetChildren()) do --cycle through all of the intvalue instances
		if v:IsA("IntValue") then
			local nameAndVal = {} --make new table to store name and value
			nameAndVal[v.Name] = v.Value --insert name and val into table as single item
			table.insert(data, nameAndVal) --insert the value of the intvalue instance into the table
		end
	end
	local res = DS:SetAsync("UserID_"..player.UserId, data) --save data to datastore
	--perform other code
end)

game:BindToClose(function() --bindtoclose executes whenever the server shuts down
	for i, player in pairs(players) do
		local data  = {}
		for i, v in pairs(storage:GetChildren()) do --cycle through all of the intvalue instances
			if v:IsA("IntValue") then
				local nameAndVal = {} --make new table to store name and value
				nameAndVal[v.Name] = v.Value --insert name and val into table as single item
				table.insert(data, nameAndVal) --insert the value of the intvalue instance into the table
			end
		end
		local res = DS:SetAsync("UserID_"..player.UserId, data) --save data to datastore
		--perform other code
	end
end)

What I’d now like to do is also save/load values inside a different folder within PlayerData called PlayerStats. How would I go about doing this?

1 Like
local DSService = game:GetService("DataStoreService")
local DS = DSService:GetDataStore("DataStore")
local players = game:GetService("Players")
local player = players.LocalPlayer or players.PlayerAdded:Wait()
local storage = player:WaitForChild("PlayerData"):FindFirstChild("Inventory")
local pdata = player:WaitForChild("PData")

players.PlayerAdded:Connect(function()
	local load = DS:GetAsync("User_"..player.UserId) --load data from datastore
	if type(load) == "table" then --expected type of value
		for i, v in pairs(load[1]) do --cycle through first primary dictionary
			for name, value in pairs(v) do --cycle through the entries of those tables
				local intVal = Instance.new("IntValue")
				intVal.Name = name
				intVal.Value = value
				intVal.Parent = player:WaitForChild("PlayerData"):FindFirstChild("Inventory") --load all the intvalue instances back into storage
				--perform other code
			end
		end
		for i, v in pairs(load[2]) do --cycle through second primary dictionary
			for name, value in pairs(v) do --cycle through the entries of those tables
				local intVal = Instance.new("IntValue")
				intVal.Name = name
				intVal.Value = value
				intVal.Parent = player:WaitForChild("PData") --load all the intvalue instances back into player
				--perform other code
			end
		end
	else
		load = {} --empty table indicates no intvalues
		--perform other code
	end
end)

players.PlayerRemoving:Connect(function(player)
	local all = {}
	local inventory  = {}
	local plrData = {}
	for i, v in pairs(storage:GetChildren()) do --cycle through all of the intvalue instances
		if v:IsA("IntValue") then
			local nameAndVal = {} --make new table to store name and value
			nameAndVal[v.Name] = v.Value --insert name and val into table as single item
			table.insert(inventory, nameAndVal) --insert the value of the intvalue instance into the table
		end
	end
	table.insert(all, inventory)
	for i, v in pairs(pdata:GetChildren()) do --cycle through all of the intvalue instances
		if v:IsA("IntValue") then
			local nameAndVal = {} --make new table to store name and value
			nameAndVal[v.Name] = v.Value --insert name and val into table as single item
			table.insert(plrData, nameAndVal) --insert the value of the intvalue instance into the table
		end
	end
	table.insert(all, plrData)
	local res = DS:SetAsync("UserID_"..player.UserId, all) --save data to datastore
	--perform other code
end)

game:BindToClose(function() --bindtoclose executes whenever the server shuts down
	for i, player in pairs(players) do
		local all = {}
		local inventory  = {}
		local plrData = {}
		for i, v in pairs(storage:GetChildren()) do --cycle through all of the intvalue instances
			if v:IsA("IntValue") then
				local nameAndVal = {} --make new table to store name and value
				nameAndVal[v.Name] = v.Value --insert name and val into table as single item
				table.insert(inventory, nameAndVal) --insert the value of the intvalue instance into the table
			end
		end
		table.insert(all, inventory)
		for i, v in pairs(pdata:GetChildren()) do --cycle through all of the intvalue instances
			if v:IsA("IntValue") then
				local nameAndVal = {} --make new table to store name and value
				nameAndVal[v.Name] = v.Value --insert name and val into table as single item
				table.insert(plrData, nameAndVal) --insert the value of the intvalue instance into the table
			end
		end
		table.insert(all, plrData)
		local res = DS:SetAsync("UserID_"..player.UserId, all) --save data to datastore
		--perform other code
	end
end)
1 Like