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

How do I save and load leaderstats?

Asked by
ElBamino 153
3 years ago

I have a leaderboard script for leaderstats that works and, I would like to save it. From everything I'm reading online I don't really understand. I'm looking for links that will help me understand and a few answers to my questions code provided below. This is in ServerScriptStorage with a Folder inside of it called leaderstats. API Services was turned on right after I made the leaderstats script.

I don't understand this, it doesn't seem to fit with mine. https://developer.roblox.com/en-us/onboarding/intro-to-saving-data/1

local players = game:GetService("Players")

local function leaderboardSetup(player)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player
    local tix = Instance.new("IntValue")
    tix.Name = "Tix"
    tix.Value = 0
    tix.Parent = leaderstats
end

players.PlayerAdded:Connect(leaderboardSetup)

while true do
    wait(1)
    local playerList = players:GetPlayers()
    for currentPlayer = 1, #playerList do
        local player = playerList[currentPlayer]
        local tix = player.leaderstats.Tix
        tix.Value = tix.Value + 1
    end
end

When I make the save/load do I put it in the same script if not, do I put it in a script or local script in ServerScriptStorage? Will this script I have work with a save/load? What can I do or what can I change? Help and explanations are appreciated. Thank you in advance.

1 answer

Log in to vote
1
Answered by 3 years ago

Here's the link the helped me with Saving & Loading data (DataStores) AlvinBlox's Tutorial

and after watching that, your code might end up looking like this

local players = game:GetService("Players")

local dataStore = game:GetService("DataStoreService")
local tixStore = dataStore:GetDataStore("TixStore")

local function leaderboardSetup(player)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player
    local tix = Instance.new("IntValue")
    tix.Name = "Tix"
    tix.Parent = leaderstats

    local data
    local success, errorMessage = pcall(function()
        data = tixStore:GetAsync(player.UserId.."-tix")    
    end)

    if success then
        tix.Value = data
    else
        warn("Error loading ".. player.Name .."'s data ".. errorMessage)
    end
end

players.PlayerAdded:Connect(leaderboardSetup)
players.PlayerRemoving:Connect(function(player)
    local data = player.leaderstats.Tix.Value
    local success, errorMessage = pcall(function()
        tixStore:SetAsync(player.UserId.."-tix", data)
    end)

    if not success then
        warn("Error saving ".. player.Name .."'s data ".. errorMessage)
    end
end)

while true do
    wait(1)
    local playerList = players:GetPlayers()
    for currentPlayer = 1, #playerList do
        local player = playerList[currentPlayer]
        local tix = player.leaderstats.Tix
        tix.Value = tix.Value + 1
    end
end
0
Thank you very much TheBigBro122, I appreciate it. The video explained better than any link I found. I'm going to do some testing and I'll let you know what I end up with. Thanks again! ElBamino 153 — 3y
1
I did some testing after watching the video so I could learn for myself. After comparing my script to yours, everything works perfect! Thanks again, very much appreciated for the help and link. ElBamino 153 — 3y
Ad

Answer this question