What is the issue? My saving system seems to not work in order.
What solutions have you tried so far? Remaking the code, but I gave up since it didn’t work either.
Greetings Roblox DevForum Community,
I am making a data saving system for my game (it is something like the steam platform, you might know it) and I have multiple values and I am storing them in folders such as “leaderstats”, “leaderstats2”, “leaderstats3” (for each value) to avoid any errors. But, I don’t know what I messed up since none of my data is saving up. The value is working correctly but it does not save. My code is down below!
local DSS = game:GetService("DataStoreService")
local TotalGames = DSS:GetDataStore("TotalGames")
game.Players.PlayerAdded:Connect(function(plr)
local pCash = TotalGames:GetAsync(plr.UserId)or 0
local leaderstats = Instance.new("Folder",plr)
leaderstats.Name = "leaderstats3"
local CashValue = Instance.new("NumberValue")
CashValue.Name = "TotalGames"
CashValue.Value = pCash
CashValue.Parent = leaderstats
CashValue.Changed:connect(function(v)
TotalGames:SetAsync(plr.UserId,v)
print("TotalGames Saved!")
end)
end)
There is not a huge necessity to keep saving the Data every time the CashValue changes within your script, cause you would be calling so much Datastore Requests that it’ll fill up your entire Output screen & potentially yield for a long time
What you can either do instead, is:
Use a PlayerRemoving Event instead to detect when the Player exactly leaves from the game to properly save the data at the right time
Every 60 seconds, call SetAsync() to save every Player’s Data provided via a loop
You should also always call Datastore functions within pcalls as well, to further debug the issue in case the Data fails to load:
local Players = game:GetService("Players")
local DSS = game:GetService("DataStoreService")
local TotalGames = DSS:GetDataStore("TotalGames")
local function AddPlr(Plr)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = Plr
local CashValue = Instance.new("NumberValue")
CashValue.Name = "TotalGames"
CashValue.Parent = leaderstats
local PotentialData
local success, whoops = pcall(function()
PotentialData = TotalGames:GetAsync(plr.UserId)
end)
if success and PotentialData then
CashValue.Value = PotentialData
print("Data successfully loaded for Player:", Plr)
else
warn("Error attempting to load the data:", whoops)
end
end
local function RemovePlr(Plr)
local CashValue = Plr:WaitForChild("leaderstats"):WaitForChild("TotalGames")
local success, whoops = pcall(function()
TotalGames:SetAsync(Plr.UserId, CashValue.Value)
end)
if success then
print("Data successfully saved for Player", Plr)
else
warn("Error attempting to save the data:", whoops)
end
end
Players.PlayerAdded:Connect(AddPlr)
Players.PlayerRemoving:Connect(RemovePlr)
And one more thing: Make sure API Services are turned on in your Game Settings to allow specific services to function correctly, such as DataStores
That’s still not considered good practice regardless, especially if you’re saving a whole bunch of data you want to keep track of
Like say you have a literal entire table full of Values that you need to save with SetAsync/UpdateAsync for every player in the server (Per under 10-15 secs), you’d be risking how much each requests can stock up the current DataStore until it gets to the limit that it starts to yield & slow down your current script
So I can use this? I am a beginner in coding and I don’t understand how to use remote events and other complicated stuff. I got my script from a combination of youtube tutorials that I could understand.
There’s still the other alternative options regardless
Calling SetAsync every so often will literally not properly save the current new data every 5-15 seconds cause so much requests are being made that it won’t load the Current Player’s last joined time, would you seriously want to have a ton of unnecessary warnings on your Output about “Datastore requests being added to queue” when you could just save it whenever a Player Leaves/Every 60 seconds? Even game:BindToClose() is an option as well
@Doom_Vickstar999 No, cause you’d be calling SetAsync for every time the Value changes & if you’re changing that Value a ton, it’s gonna result in a lot of DataStore Request warnings, try and refer to the script I posted earlier please
The thing is that I used this same script that I posted here with the leaderstats folder named “leaderstats” and with the value called “Cash” and it saved flawlessly every time. (I made a tycoon kind game)
I want to try and make my script work again (like on the other tycoon game I used it on) because this one is much simpler and I am familiar with it so it can be repaired and used. If I can’t find a solution to my script I will try yours.
Ok, I’m back from testing, it doesn’t save. It says on the server-side of the logs that the values have been saved but I rejoin to test and nope, values are at 0.
Unfortunately, some of the scripts you’re attempting to use could potentially work janky in the future & this is probably 1 of the many decent DataStore scripts out there that can do the job well
I’d be willing to break the process though
SetAsync()/GetAsync yields in an attempt to save/load the Player’s Data, there is the possibly of it erroring though so if you don’t encase it in what’s called apcallfunction (Aka Protective/Protection Call idk), the script could break entirely
Kinda think of a pcall as like preventing scripts from erroring, & say we wanted to search for a Part named “Billy” inside the workspace (But there wasn’t one, and instead we have a Part named “Steve”)
local Billy --Hi Billy!--
local success, errormessage = pcall(function()
Billy = workspace:FindFirstChild("Billy") --Attempting to find "Billy"
end)
if not success then
warn("Billy's disappeared!") --A part named "Billy" was unable to be searched inside the workspace
end