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

Is there a way to save the leaderboard?

Asked by 4 years ago
Edited 4 years ago

I'm making a simulator in Roblox but I don't know how to save the leaderboard stats. To clarify, if someone leaves and rejoins, they will have to start all the way from the beginning. This is the leaderboard script.

local DataStore = game:GetService("DataStoreService")
local PointsData = DataStore:GetOrderedDataStore("Points")

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

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

 local energy = Instance.new("NumberValue")
 energy.Name = "Energy"
 energy.Parent = leaderstats

 local coin = Instance.new("NumberValue")
 coin.Name = "LazerBux"
 coin.Parent = leaderstats

end)
0
You would have to save their stats when they leave as well as load them when they join. NotedAPI 810 — 4y
0
Yea but the thing is I don't know how to script. greasychickenugget 0 — 4y

2 answers

Log in to vote
0
Answered by 4 years ago

here's an article!

Ad
Log in to vote
0
Answered by
AIexR32 11
4 years ago
local DataStore = game:GetService("DataStoreService")
local PTS = DataStore:GetDataStore("PlayTimeStore")

game.Players.PlayerAdded:Connect(function(Player)

    local StatFolder = Instance.new("Folder",Player)
    StatFolder.Name = "leaderstats"

    local PT = Instance.new("IntValue",StatFolder) -- creating new value
    PT.Name = "PlayTime" -- changing name of value
    PT.Value = PTS:GetAsync(Player.UserId) or 0 -- loading saved value if player dont have any value then 0 
    print("Data Loaded!")
end)

game.Players.PlayerRemoving:Connect(function(Player)
    PTS:SetAsync(Player.UserId,Player.leaderstats.PlayTime.Value) -- saving data
    print("Data Saved!")
end)

this script is sample (script from my game btw) you can learn this script and make your own or get source from here

Answer this question