Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How To Save Leaderstats?

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)

1 comment

0

you save Data with the SetAsync, you access tht data with GetAsync

3 answers

✓ Accepted answer

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.

9 comments

0

And I put it into ServerScriptService right?

0

Yes.

0

You should start off with a simpler version.

0

Fine, I'll edit it to a more readable version.

0

It Keeps Printing That The Data Saved, But It Never Actually Saves It

0

It works for me.

0

Ok I probably did something wrong then Ill keep trying

0

?????????????????????

0

Btw, run it in game is better for it the debug since sometimes the local server stops when the player exited.

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)

This discussion is preserved as it was. New answers, comments, and votes are closed.