Recently I have been trying to make a game with saved stats, but none of the scripts I try work, so I made a small test place, and still the stats don’t work.
I have tried changing several things in the scripts, and I even made different datastore scripts but none of them work.
I found several different supposed solutions on the DevForum, yet none of them work.
local DataStoreService = game:GetService("DataStoreService"):GetDataStore("savestats")
game.Players.PlayerAdded:Connect(function(plr)
wait()
local plrKey = "id_"..plr.UserId
local save1 = plr.PlrStats.Level
local save2 = plr.PlrStats.Credits
local save3 = plr.PlrStats.Rank
local getSave = DataStoreService:GetAsync(plrKey)
if getSave then
save1.Value = getSave[1]
save2.Value = getSave[2]
save3.Value = getSave[3]
else
local NumberForSaving = {save1.Value, save2.Value, save3.Value}
DataStoreService:GetAsync(plrKey, NumberForSaving)
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
DataStoreService:GetAsync("id_"..plr.UserId, {plr.PlrStats.Level.Value, plr.PlrStats.Credits.Value, plr.PlrStats.Rank.Value})
end)
– NOTE –
I only listed the datastore script that works the most for me, and I did not take screenshots of the other scripts. If you want the other scripts I used, I will have to go back and find them.
game:BindToClose(function()
-- your data saving code
end)
(This also caused some of your issues)I have found a fix. Simply put your saving part of the script into this. The issue was that the player was leaving the server and it closed before saving. to fix it use DataModel | Roblox Creator Documentation so the data should be saved before the server shut down.
Main solution: Also you were using GetAsync to try to save data (GetAsync only Tell you what the data is). SetAsync saves the data. I hope this helps.
You’re calling GetAsync() instead of SetAsync() or UpdateAsync() here. GetAsync() attempts to find an entry with a key matching that of the one provided, it returns the value associated with that key. You’re also doing the same here too.
local NumberForSaving = {save1.Value, save2.Value, save3.Value}
DataStoreService:GetAsync(plrKey, NumberForSaving)
I made a datastore system not too long ago which handles everything for you. If you scroll all the way down you should find the resource, and the code to use it. Hope this helps:
oh. ok i was just trying to help people with datastores because thats something I’m good at. And most of them was leaderstats, which my module could support, so I sent it. I am also trying to figure out solutions for other datastore problems since that is what I am experienced at.