Improvements:
- When indexing a player's
user id use plr.UserId not plr.userid
- It's better to put the leaderstats and the datastore in the same script so that it saves time and uses fewer lines.
Those are the only errors/bugs I can see in your code.
Pro-tip:
When debugging code, use print statements to help you know what lines are running and what lines aren't.
Make sure this is a Normal Script.
(Also, make sure your API service is on. You can check this by going on game settings < Security and then enable it.)
Fixed Script:
local DS = game:GetService("DataStoreService"):GetDataStore("SavaData")
game.Players.PlayerAdded:Connect(function(player)
local Folder = Instance.new("Folder",player)
Folder.Name = "leaderstats"
local Coins = Instance.new("IntValue",Folder)
Coins.Name = "Coins"
local Crumbs = Instance.new("IntValue",Folder)
Crumbs.Name = "Crumbs"
local plrkey = "id_"..player.UserId
local save1 = player.leaderstats.Crumbs
local save2 = player.leaderstats.Coins
local GetSaved = DS:GetAsync(plrkey)
if GetSaved then
save1.Value = GetSaved[1]
save2.Value = GetSaved[2]
else
local NumberForSaving = {save1.Value, save2.Value}
DS:GetAsync(plrkey,NumberForSaving)
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
DS:SetAsync("id_"..plr.UserId,{plr.leaderstats.Crumbs.Value, plr.leaderstats.Coins.Value})
end)
##Recommendation(s):
It's also better to use DataModel:BindToClose, as this function gets called when the game shuts down.
<-------------------------------------------------------------------------------------------------------->
Re-edited:
(I also added another variable that references the Datastore, I also added pcalls to ensure the code runs through any errors.)
Note: You don't need to add BindToClose on line 30, you could've added it on the bottom of the script, but I just decided to do it like this:
##Re-edited Script:
local DS = game:GetService("DataStoreService")
local myDataStore = DS:GetDataStore("SaveData")
game.Players.PlayerAdded:Connect(function(player)
local Folder = Instance.new("Folder",player)
Folder.Name = "leaderstats"
local Coins = Instance.new("IntValue",Folder)
Coins.Name = "Coins"
local Crumbs = Instance.new("IntValue",Folder)
Crumbs.Name = "Crumbs"
local data
local playerUserId = "Player__" .. player.UserId
local success,errormessage = pcall(function()
data = myDataStore:GetAsync(playerUserId)
end)
if success then
if data then
Coins.Value = data.Coins
Crumbs.Value = data.Crumbs
end
end
end)
game:BindToClose(function()
print("The games has shutdown")
for i,plr in pairs(game.Players:GetPlayers()) do
local data = {
Coins = plr.leaderstats.Coins.Value;
Crumbs = plr.leaderstats.Crumbs.Value;
}
print("it works")
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local playerUserId = "Player__" .. player.UserId
local data = {
Coins = player.leaderstats.Coins.Value;
Crumbs = player.leaderstats.Crumbs.Value;
}
local success,errormessage = pcall(function()
myDataStore:SetAsync(playerUserId,data)
end)
if success then
print("Data successfully saved!")
else
print("Data failed to save!")
end
end)
If my answer helped you, please accept it!
Fixed grammatical errors
~5/12/2021
##Reference:
DataModel:BindToClose
2 comments
what exactly isnt working?
The values that are needing to be saved are returning to beggining value