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)
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")
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.