Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How To Save Leaderstats?

Asked by 4 years ago

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)
0
you save Data with the SetAsync, you access tht data with GetAsync aprilsfooled 29 — 4y

3 answers

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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.

0
And I put it into ServerScriptService right? valval0626 7 — 4y
0
Yes. LinavolicaDev 570 — 4y
0
You should start off with a simpler version. PrismaticFruits 842 — 4y
0
Fine, I'll edit it to a more readable version. LinavolicaDev 570 — 4y
View all comments (5 more)
0
It Keeps Printing That The Data Saved, But It Never Actually Saves It valval0626 7 — 4y
0
It works for me. LinavolicaDev 570 — 4y
0
Ok I probably did something wrong then Ill keep trying valval0626 7 — 4y
0
????????????????????? LinavolicaDev 570 — 4y
0
Btw, run it in game is better for it the debug since sometimes the local server stops when the player exited. LinavolicaDev 570 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

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

Log in to vote
0
Answered by 2 years ago

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)

Answer this question