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
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,
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
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
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.