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

My Datastore is not saving basic values?

Basically, I made like a bank system, but datastore is not working, i dont get any errors, anyone knows why?

local DS = game:GetService("DataStoreService"):GetDataStore("Experience")

game.Players.PlayerAdded:Connect(function(player)
	local cashMoney = Instance.new("IntValue", player)
	cashMoney.Name = "cashMoney"
	
	local bankMoney = Instance.new("IntValue", player)
	bankMoney.Name = "bankMoney"
	
	local saves = DS:GetAsync(player.UserId)
	
	if saves ~= nil then
		cashMoney.Value = saves[1]
		bankMoney.Value = saves[2]
	else
		cashMoney.Value = 0
		bankMoney.Value = 0
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local theSaveTable = {}
	
	table.insert(theSaveTable, player.cashMoney.Value)
	table.insert(theSaveTable, player.bankMoney.Value)
	
	DS:SetAsync(player.UserId, theSaveTable)
end)

1 answer

✓ Accepted answer

You forgot to save with game:BindToClose(function(). This function is called whenever the last player leaves the game (or if the game shut's down unexpectedly), so PlayerRemoving doesn't get called. The longest amount of time you can hold the server open is 30 seconds. So, in 30 seconds, everyone's data should be saved.

So, to do this all you have to do is use the function, loop through the remaining players, and save their data. For neatness, you would do this under your PlayerRemoving section.

-- ~~~~~~~~~~~~~ Player Added

-- ~~~~~~~~~~~~~ Player Removing

game:BindToClose(function()
	for _,player in pairs(game:GetService("Players"):GetPlayers()) do
		local theSaveTable = {}
		
		table.insert(theSaveTable, player.cashMoney.Value)
		table.insert(theSaveTable, player.bankMoney.Value)
		
		DS:SetAsync(player.UserId, theSaveTable)
	end
end)

Hope this helped! If it did, don't forget to mark this as an answer!

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