Best way to go about saving large amounts of player data

So I have a script that contains a large amount of player values, and I need a way to save all of the values when the player leaves, and call on those values when they rejoin. I know if I wanted to, I could just individually for each one have a line that saves them, however this seems highly inefficient and I was wondering if there was a better way to go about it.

Script that creates the values:

game.Players.PlayerAdded:Connect(function(plr)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = plr
	
	local teamXP = Instance.new("Folder")
	teamXP.Name = "teamXP"
	teamXP.Parent = leaderstats
	
	
	local CD = Instance.new("IntValue")
	CD.Name = "Class-D"
	CD.Value = 0
	CD.Parent = teamXP
	
	local FP = Instance.new("IntValue")
	CD.Name = "Foundation Personel"
	CD.Value = 0
	CD.Parent = teamXP
	
	local ScD = Instance.new("IntValue")
	ScD.Name = "Scientific Department"
	ScD.Value = 0
	ScD.Parent = teamXP
	
	local MD = Instance.new("IntValue")
	MD.Name = "Medical Department"
	MD.Value = 0
	MD.Parent = teamXP
	
	local SD = Instance.new("IntValue")
	SD.Name = "Security Department"
	SD.Value = 0
	SD.Parent = teamXP
	
	local EC = Instance.new("IntValue")
	EC.Name = "Ethics Comittee"
	EC.Value = 0
	EC.Parent = teamXP
	
	local DEA = Instance.new("IntValue")
	DEA.Name = "Department of External Affairs"
	DEA.Value = 0
	DEA.Parent = teamXP
	
	local RRT = Instance.new("IntValue")
	RRT.Name = "Rappid Response Team"
	RRT.Value = 0
	RRT.Parent = teamXP
	
	local ISD = Instance.new("IntValue")
	ISD.Name = "Internal Security Department"
	ISD.Value = 0
	ISD.Parent = teamXP
	
	local IA = Instance.new("IntValue")
	IA.Name = "Intelligence Agency"
	IA.Value = 0
	IA.Parent = teamXP
	
	local O5 = Instance.new("IntValue")
	O5.Name = "O5 Council"
	O5.Value = 0
	O5.Parent = teamXP
	
	local CI = Instance.new("IntValue")
	CI.Name = "Chaos Insurgency"
	CI.Value = 0
	CI.Parent = teamXP
	
end)

A table.

Store them all in a table, and simply save that table. It should only be 1 GetAsync and 1 SetAsync.

Could you post a simple example, please?

I believe there are tons of posts about this , sorry - on mobile, would take me cople of minutes to type it down.

--saving
--put this after the leaderstats creation, inside of it.
local data = {Cash = Cash.Value, Gems = Gems.Value)

DataStore:GetAsync(Player.UserId, data)


--loading


local data = {Cash = Player.leaderstats.Cash.Value, Gems = Player.leaderstats.Gems.Value)

DataStore:SetAsync(Player.UserId,data)

Dont forget to also use game:BindToClose.

Make sure to search up this topic on goggle to find more solutions.

1 Like

Should I use game:BindToClose for setting async, or Player:PlayerRemoving?

Both. BindToClose is for cases when the game gets shut-down, etc.
In the BindToClose, you’d prob need to loop through all players and save for each of them.

I believe there are posts on that too.

Alright, thank you once again.