Data store saving script only saving 1 of the 3 values

Hello! Im new to data stores and Im trying to save 3 leader board values, Coins, Presses, and Minutes

the problom is, the script only saves coins.
I have it print after it saves each one and it does so, but still, only the coins save.
I have no idea whats wrong. heres the script.

– At the time of posting this Im at school so my responds will be slow –

 local datastore = game:GetService("DataStoreService")
local Store1 = datastore:GetDataStore("Coins")
local Store2 = datastore:GetDataStore("Presses")
local Store3 = datastore:GetDataStore("Minutes")

game.Players.PlayerAdded:connect(function(plr)
	local LeaderStatsFolder = Instance.new("Folder", plr)
	local Store1Value = Instance.new("IntValue", LeaderStatsFolder)
	local Store2Value = Instance.new("IntValue", LeaderStatsFolder)
	local Store3Value = Instance.new("IntValue", LeaderStatsFolder)


	LeaderStatsFolder.Name = "leaderstats"	
	-------------------------------------------------------------------------------

	Store1Value.Name = "Coins"
	Store2Value.Name = "Presses"
	Store3Value.Name = "Minutes"
	local DebugPrint = game.ReplicatedStorage.DebugStuff.PrintDebugStuff.Value

	-------------------------------------------------------------------------------

	Store1Value.Value = Store1:GetAsync(plr.UserId) or 0
	Store1:SetAsync(plr.UserId, Store1Value.Value)

	Store2Value.Value = Store2:GetAsync(plr.UserId) or 0
	Store2:SetAsync(plr.UserId, Store2Value.Value)

	Store2Value.Value = Store3:GetAsync(plr.UserId) or 0
	Store3:SetAsync(plr.UserId, Store3Value.Value)

	-------------------------------------------------------------------------------

	while true do
		wait(33)
		Store1:SetAsync(plr.UserId, Store1Value.Value)
		if DebugPrint == 1 then
			print("Saved Coins")
		end
		wait(33)
		Store2:SetAsync(plr.UserId, Store2Value.Value)
		if DebugPrint == 1 then
			print("Saved Button Presses")
		end
		wait(33)
		Store3:SetAsync(plr.UserId, Store3Value.Value)
		if DebugPrint == 1 then
			print("Saved Total Time Played")
		end
	end
end)

In your output log do you receive anything about datastore requests being queued?
Also it would be better to just store all the values in a table so then you wouldn’t be using all of your data requests per minute so quickly

No I have not gotten anything about the requests being queued or anything signaling there is a problom.

Have you printed what the datastore has stored to know that it is actually saving? Also do you have API services enable so that you can use Datastore Service?

Yes API services are on and I will test printing what the data store stored

Perhaps you can take a look at this? I believe it will be of good use/help to you!

I’ll try this.

Why do I need 30 letters

They are all good, but the one I used was by @piggy313 Useful Solution

1 Like

After it saved I printed the saved value and for coins it printed the amount but for minutes and button presses it just printed 0 no matter what there values are

-------------------------------------------------------------------------------

	Store1Value.Value = Store1:GetAsync(plr.UserId) or 0
	Store1:SetAsync(plr.UserId, Store1Value.Value)

	Store2Value.Value = Store2:GetAsync(plr.UserId) or 0
	Store2:SetAsync(plr.UserId, Store2Value.Value)

	Store2Value.Value = Store3:GetAsync(plr.UserId) or 0
	Store3:SetAsync(plr.UserId, Store3Value.Value)

	-------------------------------------------------------------------------------

Add some prints around here to figure out what’s going on I have a feeling since you aren’t retrieving data in protected calls a.k.a Pcalls

I will try that but i have to go soon I will be able to respond in about 5 hours once school is done for me

Creating an example of using Tables for your datastores almost done

Decided to just put the thing together for you, I see you’re new to DataStore so I want to help!

I hope this will help you, learn from it and change accordingly.
Save/Load Data

1 Like

The Table example i was refering to

PlayerData in a table

local Players = game:GetService("Players")
local DatastoreService = game:GetService("DataStoreService")
local PlayerDataStore = DatastoreService:GetDataStore("PlayerData")
	
local PlayerDataTable  = {
	Coins = 0,
	Presses = 0,
	Minutes = 0,
}
	

	
	
function GetData(Player)
	if not Player return end -- if player is not in game end function
	
	local Data
	
	local Success, Response = pcall(function()
		Data = PlayerDataStore:GetAsync(Player.UserId)
	end)
	
	if Success and not Data then --// If Data Retrival Sucessful and Data == nil
		warn(Player.Name .. " has no Data ... Setting Up Datastore")
	
		local DataTable = PlayerDataTable
		
		return DataTable
	else --// If Data Retrival Sucessful and Data ~= nil
			warn(Player.Name .. " hasData ... Data Retrieval Successful")
		return Data
	end
end
	
	
function UpdateData(Player)
	if not Player return end -- if player is not in game end function

	local LeaderStatsFolder = Player.leaderstats 

	local Coins = LeaderStatsFolder.Coins
	local Presses = LeaderStatsFolder.Presses
	local Minutes = LeaderStatsFolder.Minutes

	local DataTable = PlayerDataTable

	local Success, Response = pcall(function()
	
		PlayerDataStore:UpdateAsync(Player.UserId, function(OldTable)
			local NewTable = DateTime --// Unintentional error / forgot to make update table
			return NewTable
		end)
	end)
end
	
	
	
Players.PlayerAdded:connect(function(Player)
--[[ local LeaderStatsFolder = Instance.new("Folder", plr) 
First it isnt recommended to use the Second Param of Instance.new
 You want to set the instances properties before its Parent
]]
		local LeaderStatsFolder = Instance.new("Folder")
			LeaderStatsFolder.Name = "leaderstats"	
			LeaderStatsFolder.Parent = Player
		
		local Store1Value = Instance.new("IntValue")
			Store1Value.Name = "Coins"  
			Store1Value.Parent = LeaderStatsFolder
		
		local Store2Value = Instance.new("IntValue") 
			Store2Value.Name = "Presses" 
			Store2Value.Parent = LeaderStatsFolder
		
		local Store3Value = Instance.new("IntValue")
			Store3Value.Name = "Minutes" 
			Store3Value.Parent = LeaderStatsFolder



			-------------------------------------------------------------------------------

			local DebugPrint = game.ReplicatedStorage.DebugStuff.PrintDebugStuff.Value

			-------------------------------------------------------------------------------
	local Data = GetData(Player)
				Store1Value.Value = Data.Coins
				Store2Value.Value = Data.Presses
				Store3Value.Value = Data.Minutes

			-------------------------------------------------------------------------------

	while true do
		wait(30) -- // If wait is anyless than 30 seconds it will be overkill
		UpdateData(Player)
	end

end)
Error in the UpdateData function (i didn't even make the updatedata table at all)
function UpdateData(Player)
	if not Player return end -- if player is not in game end function

	local LeaderStatsFolder = Player.leaderstats 

	local Coins = LeaderStatsFolder.Coins
	local Presses = LeaderStatsFolder.Presses
	local Minutes = LeaderStatsFolder.Minutes

	local Success, Response = pcall(function()
	
		PlayerDataStore:UpdateAsync(Player.UserId, function(OldTable)
			local NewTable = {Coins = Coins.Value, Presses = Presses.Value,Minutes = Minutes.Value } -- I didn't even make the update Datatable mybad
			return NewTable
		end)
	end)
end

Updating Data when PlayerRemoving and ServerShutting Down
Players.PlayerRemoving:Connect(function(Player)
UpdateData(Player) -- Will Save the Data of the player leaving
end)


game:BindToClose(function()--//this function for saving data when game is shuting down

	local players = Players:GetPlayers()
	for _, Player in pairs(players) do
		UpdateData(Player)
	end
end)

I was rushing and forgot to make the update data table mybad.

2 Likes

They you very much I will try this later, this will also be a very good reference for me to use to figure out how exactly I should handle data stores.

1 Like

Thank you this will be very use full for learning how exactly to handle data stores

Hello again! I’m back from school and was testing the script and realized, it doesn’t seem to save. I’ve looked through it and have no clue why it’s not saving. I’ve added a print for every time it saves and yet, I stop and play again and it just reset back to 0 and sets up the datastore again. Could you help me with this? I’m clueless at this point.

But the script I’m using saves every 30 seconds but either way, I also made sure to test it out of studios and still, it never saved.

Oh, sorry. I must have missed that part.