Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to save 2 Data Stores via Tables?

I know how Datastore work, and how to save on value, eg Wins. But when I try to save 2 Values with it, It doesn't work, I got some help yesterday but it wasn't really helpful, as I am not familiar with Saving data with Tables/Arrays

This is how to Load Players Data, but when he leaves, what should be done, would appreciate in depth help <3

local dataTable = {
    Points = Points.Value
    Wins = Wins.Value
}

local success, err = pcall(function()
    DATASTORE:GetAsync(dataTable)
end)
if success then
    Points.Value = dataTable.Points
    Wins.Value = dataTable.Wins
end

1 answer

✓ Accepted answer

When he leaves you are obviously going to want to save the data. The best way to do this is in a PlayerRemoving function. A function that runs when the player leaves the game.


game.Players.PlayerRemoving:Connect(function(Player)


	

end)

The next thing your going to want to do is reference the data. I personally do not know how your data is stored so the next part will likely need to be edited depending on your situation.


game.Players.PlayerRemoving:Connect(function(Player)

--This is based off the assumption your data is stored like this. Obviously your data might be saved differently so you'll want to correctly reference it in the table. (I can't just type Points.Value because its a separate function obviously.)

local Data = {
		
		Points = Player.leaderstats.Points.Value
		Wins = Player.leaderstats.Wins.Value
}


end)

The final part you are going to want to do is save this to the player. To do this you are going to need a key. Something which you can identify for each individual player. You are then going to need to try and save that (doing it in a pcall in case it fails).

The final result should look something like this:


--This Gets The Data Store:

local DataStoreService = game:GetService("DataStoreService")

--This Creates The Game Data Store With The Name Of Game Data:

local GameDataStore = DataStoreService:GetDataStore("GameDataStore")

game.Players.PlayerRemoving:Connect(function(Player)

--This is based off the assumption your data is stored like this. Obviously your data might be saved differently so you'll want to correctly reference it in the table. (I can't just type Points.Value because its a separate function obviously.)

local Data = {
		
		Points = Player.leaderstats.Points.Value
		Wins = Player.leaderstats.Wins.Value
}

--This is the key:
local PlayerUserId = "Player_"..Player.UserId

--Wrapped in a pcall this saves the data to the data store
	local Success, ErrorMessage = pcall(function()
		GameDataStore:SetAsync(PlayerUserId, Data)
	end)
	
	
	--We know the datas saved:
	if Success then
		print("Data Saved.")
	else
	--Data didn't save:
		print(ErrorMessage)
	end	
end)

When loading the data you are obviously going to need to reference the key. However since this question was purely about saving data on removal I will only be answering that. (+ if I did much more I would practically be scripting your data save system)

1 comment

0

This is all I needed, thank you so much for taking your time and describing everything!

This discussion is preserved as it was. New answers, comments, and votes are closed.