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 leaderboard stats?

Asked by 9 years ago

How do I save the values/string of a leaderboard stat and have it load the next time the player joins the game? The following is the script I will be using for my game:

game.Player.PlayerAdded:connect(plr)
    local leaderstats = Instance.new("Model",plr)
    leaderstats.Name = "leaderstats"

    local lvl = Instance.new("IntValue", leaderstats)
    lvl.Name = "LVL"
    lvl.Value = 1

    local currency = Instance.new("IntValue", leaderstats)
    currency.Name = "Gold"
    currency.Value = 100
end)

I would like you to edit this script so that it could save and load the next time the player joins. I would also like you to elaborate the workings of saving and loading values (when I tried to check the roblox wiki, something about Data store came up but I don't really understand).

1 answer

Log in to vote
3
Answered by 9 years ago

Heres to save, load data.. http://wiki.roblox.com/index.php?title=DataStore

This should teach you about saving / loading data. ;D

http://wiki.roblox.com/index.php?title=API:Class/GlobalDataStore/UpdateAsync ROBLOX's example :

local DataStore = game:GetService("DataStoreService"):GetDataStore("Example")

game.Players.PlayerAdded:connect(function(player)
    local key = "user_" .. player.userId
    --we want to give 50 points to users each time they visit
    DataStore:UpdateAsync(key, function(oldValue)
        local newValue = oldValue or 0 --oldValue might be nil
        newValue = newValue + 50
        return newValue
    end)
end)

````````


local DS = game:GetService("DataStoreService"):GetDataStore("Points") game.Player.PlayerAdded:connect(plr) local leaderstats = Instance.new("Model",plr) leaderstats.Name = "leaderstats" local lvl = Instance.new("IntValue", leaderstats) lvl.Name = "LVL" lvl.Value = 1 local currency = Instance.new("IntValue", leaderstats) currency.Name = "Gold" currency.Value = 100 while wait(5) do DS:SetAsync(plr.userId.."_DS", currency.Value) end end)
0
Thanks! :D MaplebloxX54321 15 — 9y
0
Can you atleast give me what I wanted for my script? MaplebloxX54321 15 — 9y
0
Sure. MessorAdmin 598 — 9y
0
Question: for the while wait(5) do part, does that save the currency value every 5 seconds and not everything else? MaplebloxX54321 15 — 9y
View all comments (10 more)
0
Yea, you would have to set up nother datastore table or something to make it save the lvl. MessorAdmin 598 — 9y
0
So... Basicly the same as lines 15-17, except that instead of currency.Value, it's lvl? MaplebloxX54321 15 — 9y
0
yes MessorAdmin 598 — 9y
0
Thanks! :D MaplebloxX54321 15 — 9y
0
np MessorAdmin 598 — 9y
0
Accept my answer please! :D MessorAdmin 598 — 9y
0
4 Years And Your Answer Wasn't Accepted But Works. EnzoTDZ_YT 275 — 4y
1
Another year goes by and his answer still hasn't been accepted, sadness. CodedStars 70 — 3y
0
Truth CodedStars lol. MessorAdmin 598 — 2y
0
Truth CodedStars lol. MessorAdmin 598 — 2y
Ad

Answer this question