DataStore script not working

Hello, I’m trying to make a save system for a stat in my game, but the data is not saving at all. I’m not sure if this is because the data is not being retrieved correctly or because it’s not saving and I have turned on API services. Thank you for helping! (Script below)

I also found that when playing and publishing the game to Roblox, the data would save.

local datastoreservice = game:GetService("DataStoreService")
local CurrencyDataStore = datastoreservice:GetDataStore("CurrencyDataStore")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local Coins = Instance.new("IntValue")
	Coins.Name = "Coins"
	Coins.Parent = leaderstats
	Coins.Value = 0

	local Tickets = Instance.new("IntValue")
	Tickets.Name = "Tickets"
	Tickets.Parent = leaderstats
	Tickets.Value = 0
	local data
	local boolean = false
	local s,e = pcall(function()
		data = CurrencyDataStore:GetAsync(player.UserId .."-Leaderstats")
		if data then boolean = true end
	end)

	if boolean == true then
		Tickets.Value = data
	else
		Tickets.Value = 0
	end

	while true do
		wait(1)
		Coins.Value = Coins.Value + 1
		Tickets.Value = Tickets.Value + 1
	end

end)

game.Players.PlayerRemoving:Connect(function(player)
	local Tickets = player.leaderstats.Tickets.Value
	print("saving..")
	local s,e = pcall(function()
		CurrencyDataStore:SetAsync(player.UserId .."-Leaderstats", Tickets)
	end)
end)

Remove this part of the code:

local data
local boolean = false
local s,e = pcall(function()
	data = CurrencyDataStore:GetAsync(player.UserId .."-Leaderstats")
	if data then boolean = true end
end)
if boolean == true then
	Tickets.Value = data
else
	Tickets.Value = 0
end

And now instead
You can use:
Tickets.Value = 0 or CurrencyDataStore:GetAsync(player.UserId .."-Leaderstats")

That’s actually bad practice. You should always wrap getting data in a pcall. The Boolean part might be weird, but they should use a pcall.

The server may shut down before the data can save, try adding game:BindToClose() so it can hold the server open like this:

game:BindToClose(function()
task.wait(5) -- waits 5 seconds before the server shuts down
end)

I think it’s the loop that’s causing the problem, maybe this will work.

local datastoreservice = game:GetService("DataStoreService")
local CurrencyDataStore = datastoreservice:GetDataStore("CurrencyDataStore")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local Coins = Instance.new("IntValue")
	Coins.Name = "Coins"
	Coins.Parent = leaderstats
	Coins.Value = 0

	local Tickets = Instance.new("IntValue")
	Tickets.Name = "Tickets"
	Tickets.Parent = leaderstats
	Tickets.Value = 0
	local data
	local s,e = pcall(function()
		data = CurrencyDataStore:GetAsync(player.UserId .."-Leaderstats")
	end)

	if data then
		Tickets.Value = data
	else
		Tickets.Value = 0
	end

	task.spawn(function()
		while true do
			wait(1)
			Coins.Value = Coins.Value + 1
			Tickets.Value = Tickets.Value + 1
		end
	end)	--()
end)

game.Players.PlayerRemoving:Connect(function(player)
	local Tickets = player.leaderstats.Tickets.Value
	print("saving..")
	local s,e = pcall(function()
		CurrencyDataStore:SetAsync(player.UserId .."-Leaderstats", Tickets)
	end)
end)

Hello!
You should test DataStores on ROBLOX platform, since testing them on Roblox Studio can cause some errors to the DataStore (which is REALLY bad). If you want to test DataStores in Studio then you should create a Test version of your experience. And~ about the script, I don’t see the problem here, maybe it is the loop, as @P1po666 said, so you can use task.spawn() or Coroutines.

Do you mean that the datastore script might work if I play the game on Roblox and not if I play on Studio or that I can accidentally corrupt my data or smth in studio?

If your experience is being played while you are testing your experience and you save something in your DataStore, you could overwrite client’s Data because Studio access to the same DataStores as the client application. So it would be better and safer creating a test version or just testing it on Roblox. What I know is that it can cause some errors, so yeah, that could corrupt your data.

TYSM! I tested it in Roblox and it worked normally :smiley: !

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.