Here is the script I am using for the currency, it works fine, but I've looked at many tutorials on how to save it, and I can't figure it out.
local function onPlayerJoin(player) local leaderstats = Instance.new("Folder") leaderstats.Name = "leaderstats" leaderstats.Parent = player local cash = Instance.new("IntValue") cash.Name = "Cash" cash.Value = 50 cash.Parent = leaderstats end game.Players.PlayerAdded:Connect(onPlayerJoin)
One of the saving scripts that I tried was this:
local DataStoreService = game:GetService("DataStoreService") local myDataStore = DataStoreService:GetDataStore("myDataStore") game.Players.PlayerAdded:Connect(function(player) local leaderstats = Instance.new("Folder") leaderstats.name "leaderstats" leaderstats.Parent = player local cash = Instance.new("IntValue") cash.Name = "Cash" cash.Parent = leaderstats local data local success, errormessage = pcall(function() data = myDataStore:GetAsync(player.UserId.."-cash") end) if success then cash.Value =data else print("There Was An Error While Getting Your Data") warn(errormessage) end end) game.Players.PlayerRemoving:Connect(function(player) local success, errormessage = pcall(function() myDataStore:SetAsync(player.UserId.."-cash", player.leaderstats.Cash.Value) end) if success then print("Player Data Succesfully Saved!") else print("There Was An Error When Savving Data") warn(errormessage) end end)
There are several problems, messed up with a bunch of code.
1.You need an error list for error players.
2.You need to retry if the player is not in error.
3.Correct the syntax errors.
local DataStoreService = game:GetService("DataStoreService") local DefaultCash = 100 local RETRY_LIMIT = 5--Retry. local myDataStore = DataStoreService:GetDataStore("myDataStore") local ErrorPlayers = {}--You need an error list for error players. game.Players.PlayerAdded:Connect(function(player) local leaderstats = Instance.new("Folder") leaderstats.Name = "leaderstats" leaderstats.Parent = player local cash = Instance.new("IntValue") cash.Name = "Cash" cash.Parent = leaderstats local Retries = 0 local data local function TryFunction() data = myDataStore:GetAsync(player.UserId.."-cash") end local success, errormessage = pcall(TryFunction) if success and data ~= nil then cash.Value = data elseif success == true then--First time print('First time joining.') cash.Value = DefaultCash elseif success == false then--Error print('An error occurred when trying to get ' .. player.Name .. "'s data, retrying...") repeat wait(0.5) success, errormessage = pcall(TryFunction) Retries = Retries + 1 print('Retrying ' .. Retries) until Retries >= RETRY_LIMIT or success if success then cash.Value = data else print("Player " .. player.Name .. " added to error players.") ErrorPlayers[player.Name] = true end end end) game.Players.PlayerRemoving:Connect(function(player) if ErrorPlayers[player.Name] ~= true then local success, errormessage = pcall(function() myDataStore:SetAsync(player.UserId.."-cash", player.leaderstats.Cash.Value) end) if success then print("Player Data Succesfully Saved!") else print("There Was An Error When Saving Data.") warn(errormessage) end else--If the player is inside error print("Not saving " .. player.Name .. "'s data because of the error list.") ErrorPlayers[player.Name] = false--Remove from error list when leaving end end)
I fixed it. You can look at the articles here.
at looking at this over the top you are most likely creating two different "leaderstats" your first script is creating leaderstats and your datastore script is also creating a leaderstats
something like this should work:
game.Players.PlayerAdded:Connect(function(plr) local ls = Instance.new("Folder") ls.Name = "leaderstats" ls.Parent = plr local cash = Instance.new("IntValue") cash.Name = "Clicks" cash.Parent = ls local ras = Instance.new("IntValue") ras.Name = "Rebirths" ras.Parent = ls local data = nil local data2 = nil pcall(function() data = ds:GetAsync(plr.UserId .. "-Clicks") data2 = ds:GetAsync(plr.UserId .. "-Rebirths") end) cash.Value = data or 0 ras.Value = data2 or 0 end) game.Players.PlayerRemoving:Connect(saveData) game:BindToClose(function() for i, plr in pairs(game.Players:GetPlayers()) do saveData(plr) end end)