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

How do I use datastore with Leaderboards?

Asked by 10 years ago

I have been having some trouble with DataStore lately, I know it's possible to do it with a leaderboard, but how?

The value I want to save is called Points and it is located in a model called leaderstats inside a player.

Here is the script.

local playerStats = {} --this keeps a list of the stats for each player that enters the game

 game.Players.PlayerAdded:connect(function(player) --Added function
     local leaderstats = Instance.new("Model", player) 
     leaderstats.Name = "leaderstats" 

        local Points = Instance.new("StringValue", leaderstats)
            Points.Name = "Points"
                 Points.Value = 0




local grouprank = Instance.new("StringValue", leaderstats) 
       grouprank.Name = "Rank" 
       local rank = player:GetRoleInGroup(1066846)  
     local rankn = player:GetRankInGroup(1066846)
     grouprank.Value = rank 

        if rankn == 75 then 
        local team = game:GetService("Teams") 
        player.TeamColor = team.DJ.TeamColor
        playerStats[player] = leaderstats 
     end
end)

--[[
-refrences-

leaderstats          Model inside the player holding the values.
Points               String value in the leaderstats model  
Rank                 String value in the leaderstats model  


--]]
0
I do have a script that might help you out. It adds 50 points everytime a player joins. It saves and loads points . Its in my models on roblox, check it out yurhomi10 192 — 10y
0
Well, I want to make it so the rank is a stat too, but not save that. And I don't want it to add 50 points, also I would like an explanation so I learn something. :) IntellectualBeing 430 — 10y

1 answer

Log in to vote
1
Answered by
Jonibus 30
10 years ago

Well the 2 prime ways of saving and loading data with DataStore would be: GetAsync for retrieving data UpdateAsync for saving data

All DataStore events/methods must be called on the datastore service so it is recommended you define that as a variable straight away. Then, when the player joins the game, you can use a ternary operator with GetAsync to set values.

As for when a player leaves the game, you can use UpdateAsync to save the values. BUT I recommend you add a game.OnClose function for when the last player leaves the server because it doesn't always save. Here is an example with the leaderstat you provided:

ds = game:GetService("DataStoreService"):GetDataStore("Points") -- GetDataStore provides a key to save everything under.  Can be anything.

game.Players.PlayerAdded:connect(function(player)
    local leaderstats = Instance.new("IntValue",player) -- Creating the leaderboard
    leaderstats.Name = "leaderstats"
    local points = Instance.new("IntValue",player)
    points.Name = "Points"
    points.Value = ds:GetAsync(player.Name) or 0 -- Here we look for the players name under "Points".  DataStores act like a table, and if the players name doesn't yet exist, it sets the points to 0 by default.
end)

game.Players.PlayerRemoving:connect(function(player)
    ds:UpdateAsync(pplayer.Name, function(oldValue) return p.leaderstats.Points.Value end) -- This can be confusing.  The first argument, p.Name, is the name of the key inside the Points DataStore.  The value you update it with has to be a function, so function(oldValue) return value end is the recommended way to go.  The argument of the function is what the value used to be
end)

game.OnClose = function() -- This part is recommended to ensure the game saves when the last player leaves the server before it shuts down!
    for i,v in pairs(game.Players:GetChildren()) do
        ds:UpdateAsync(v.Name, function(oldValue) return v.leaderstats.Points.Value end)
    end
    wait(1)
end)

For any further help contact me on ROBLOX at Jonibus or look up Data Store on the ROBLOX wiki!

0
For anyone reading this: NEVER, EVER USE PLAYER NAMES ON DATASTORES, USE USERID'S. IF YOU USE NAMES, A PLAYER WHO CHANGED THEIR USERNAME WILL LOSE ALL THEIR DATA!! radzik9988 -7 — 3y
Ad

Answer this question