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

Why arent my leaderstats saving? (or loading, Im not sure)

There are no errors in the output but when I exit and enter the game again, my stats dont load. the script is a server script inside ServerScriptService. Here is my code:

local DataStore = game:GetService("DataStoreService")
local ds = DataStore:GetDataStore("CashSaveSystem")
local ds2 = DataStore:GetDataStore("ExperienceSaveSystem")
local ds3 = DataStore:GetDataStore("LevelSaveSystem")

game.Players.PlayerAdded:connect(function(player)

    local leader = Instance.new("Folder",player)
    leader.Name = "leaderstats"

    local currency = Instance.new("IntValue",leader)
    currency.Name = "Coins"
    currency.Value = ds:GetAsync(player.UserId)
    ds:SetAsync(player.UserId, currency.Value)
    currency.Changed:connect(function()
        ds:SetAsync(player.UserId, currency.Value)
    end)

    local experience = Instance.new("IntValue",leader)
    experience.Name = "Exp"
    experience.Value = ds2:GetAsync(player.UserId)
    ds2:SetAsync(player.UserId, experience.Value)
    experience.Changed:connect(function()
        ds2:SetAsync(player.UserId, experience.Value)
    end)

    local experiencelevel = Instance.new("IntValue",leader)
    experiencelevel.Name = "Level"
    experiencelevel.Value = ds3:GetAsync(player.UserId)
    ds3:SetAsync(player.UserId, experiencelevel.Value)
    experiencelevel.Changed:connect(function()
        ds3:SetAsync(player.UserId, experiencelevel.Value)
    end)
end)

game.Players.PlayerRemoving:connect(function(player)
    ds:SetAsync(player.UserId, player.leaderstats.Coins.Value)
    ds2:SetAsync(player.UserId, player.leaderstats.Exp.Value)
    ds3:SetAsync(player.UserId, player.leaderstats.Level.Value)
end)

1 comment

0

have you tried actually going into the physical game rather than studio, a lot of the times when mine wouldn't load it was because it just wouldn't save inside of studio but worked fine in the actual game

1 answer

If It doesn't work in the actual physical game, maybe try a different approach. I believe I have used that way of saving it before and I don't think it saved mine either. This is my current leaderstats script (I only have one variable though so obviously if you wanted to use this you would have to change it up a bit). This one also doesn't save in the studio but will save in the actual game. Hope it helps. My leaderstat script:

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 don't believe my prints work because I messed something up there but the actual leader stat does save in the game. I got this from AlvinBlox youtube channel and changed a few things with the help of some other people too. But always try loading into the game rather than just testing it in the studio. And I'm sure you have but make sure API Services are enabled.

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