Im trying to make my simulator save but my code is not working no errors show up when tested.
local DS = game:GetService("DataStoreService"):GetDataStore("SavaData") game.Players.PlayerAdded:Connect(function(plr) wait() local plrkey = "id_"..plr.userId local save1 = plr.leaderstats.Crumbs local save2 = plr.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)
Improvements:
user id
use plr.UserId
not plr.useridThose 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)
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:
local DS = game:GetService("DataStoreService") local myDataStore = DS:GetDataStore("SaveData") -- Added 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
If there aren't errors, then its possible this won't work because your using studio. When a player leaves, it fires player removing before the server can shut down, this is normal roblox. But with studio, when you hit the stop button, it shuts down the virtual server so it won't actually run player removing most of the time. I have experienced it myself this week and many times before. To make sure it saves, you can save it manually while using studio, make a button or something which upon clicking, will fire to a remote event that saves the data before you click stop which closes the virtual server.
Hope this helps :3