(Solved) Can't save leaderstats

hello,
I wanted to make a quest system in my game so I tried to add a saving leaderstat. I tried 3 Scripts, and none of them worked. This is the last script I tried:

local dataStore = game:GetService(“DataStoreService”):GetDataStore(“MoneyData”)

starterMoney = 0

game.Players.PlayerAdded:Connect(function(plr)

local leaderstats = Instance.new(“Folder”)
leaderstats.Name = “questvalues”
leaderstats.Parent = plr

local money = Instance.new(“IntValue”)
money.Name = “quest1”
money.Value = dataStore:GetAsync(plr.UserId) or starterMoney
money.Parent = leaderstats

end)

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

dataStore:SetAsync(plr.UserId, plr.questvalues.quest1.Value)

end)

The IntValue is there but it isn’t saving. How can I fix this problem?

Note: The folder’s name isn’t leaderstats because I don’t want the leaderstat to show up on the leaderboard.

Also Sorry if I made a mistake in English.

2 Likes

have you tested in real server? the studio test barely saves/load so it is a bad place to test your datastore saves

Yes I tested it on a real server.

you should instead wrap the GetAsync in a pcall and define data as a variable to set the given datastore saves to the data, then if it succeeds, then you compare data to starterdata,

Can you tell it simpler?
(30 char)

you should wrap the GetAsync function in a pcall because you may fail at certain times and instead compare a variable to the starterdata instead of compare a function to the starterdata for better performance.

local data
local success,errmessage = pcall(function()
    data = getasync(yoursave)
end)
if success then
    intvalue.Value = data or starterdata
end
--just an example

Still not working :frowning:

Also if I’m not mistaken it should be something like this:
local data
local success,errmessage = pcall(function()
data = dataStore:GetAsync(plr.UserId)
end)
if success then
money.Value = data or starterMoney
end

a shot in the dark but try adding

game:BindToClose(function()
	wait(3)
end)
1 Like

Where should I add this code?

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

game:BindToClose(function()
	dataStore:SetAsync(plr.UserId, plr.questvalues.quest1.Value)
	wait(3)
end)

end)

This is what I did.

Yust a tip:
Every time you use something with “Async” use a pcall:

pcall(function() 
    ContentProvider:PreloadAsync(Thing,function() end)
    DataStore:SetAsync(UserId, Data)
    local Thing = DataStore:GetAsync(UserId)
end)

Yust worth noting. (Note i my self forget it some time)

1 Like

I found a better script which saves when the leaderstat is changed. Guess this will work :slight_smile:

If You Are Testing It On Studio side, do you have API Services checked?

yes
I tested it on both studio and roblox.

1 Like

I solved it myself. The problem is filteringenabled. When I change quest1’s value, It only changes on the client. I used remote events to make it change on the server.