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

How do I make the points for players on my in game leader board save for the next visit?

Asked by 5 years ago

So i set up an in-game leaderboard using this script

-- In 'ServerScriptService', add this into a script named 'PlayerSetup'

local function onPlayerJoin(player)

local leaderstats = Instance.new("Folder")

leaderstats.Name = "leaderstats"

leaderstats.Parent = player

-- Display an 'IntValue' on leaderboard

local points = Instance.new("IntValue")

points.Name = "Points"

points.Value = 0

points.Parent = leaderstats

end

-- Run 'onPlayerJoin()' when the 'PlayerAdded' event fires

game.Players.PlayerAdded:Connect(onPlayerJoin)

and I have a way of giving players points, but how do I make it so that you keep these points when you leave and come back?

0
hey is anyone gonna answer? testing34545 12 — 5y
1

1 answer

Log in to vote
0
Answered by
awfulszn 394 Moderation Voter
5 years ago
Edited 5 years ago

Simple answer: DataStore.

Explanation:

DataStore is used to save pretty much any values you'd like, sometimes even models and such. It is essentially a table, where each value inside it can be indexed by a unique key, it can be anything you'd like, however it must be unique, such as a player's username or user ID.

Now that I've explained that, let's get down to coding said DataStore.

Coding:

Since DataStore is managed by the DataStoreService, we're gonna wanna define this as a variable, along with the name of our DataStore.

local DSService = game:GetService('DataStoreService'):GetDataStore('Points')

You'll notice I named the DataStore 'Points', this can be named anything you'd like, however changing it will wipe all data that's saved within it.

Now, we'll want this to run as soon as a player joins the game, we can do this by connecting to a function as soon as somebody joins.

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

This just waits for a player to join, and once they do, the code inside of the function will run.

We should now define the location of our leaderstats, for ease later on.

local leaderstats = plr:WaitForChild('leaderstats')

Now we are able to create our unique key, and define the value we want to be saved. For the unique key, we can use the userId, since each one is different for each player.

local pointsSave = 'points-'..plr.userId
local savePointsValue = leaderstats.Points

Now, once a player joins, we'll want them to have their saved data, if any is available, and we can do this by checking to see if they have any available, and if it is then we'll set the value of their Points to what was previously saved, and if they do not have any saved data, then we can add them to the DataStore.

local getSavedPoints = DSService:GetAsync(pointsSave)
if getSavedPoints then
    savePointsValue.Value = getSavedPoints[1]
else
    local pointsForSaving = {savePointsValue.Value}
    DSService:SetAsync(pointsSave, pointsForSaving)
end

So, we're not completed yet, however the following code should load any saved data, and give it to the player once they join:

local DSService = game:GetService('DataStoreService'):GetDataStore('Points')

game.Players.PlayerAdded:Connect(function(plr)
    local leaderstats = plr:WaitForChild('leaderstats')

    local pointsSave = 'points-'..plr.userId
    local savePointsValue = leaderstats.Points

    local getSavedPoints = DSService:GetAsync(pointsSave)
    if getSavedPoints then
        savePointsValue.Value = getSavedPoints[1]
    else
        local pointsForSaving = {savePointsValue.Value}
        DSService:SetAsync(pointsSave, pointsForSaving)
    end
end)

But, we'll still need to save their data once they leave, so it will make it available once they join, else they would lose all progress.

We previously used PlayerAdded to run some code once a player joined the game, but now we can use the opposite: PlayerRemoving.

Just like PlayerAdded, this will run the code inside too, except only as a player is leaving the game.

game.Players.PlayerRemoving:Connect(function(plr)

Once again, we're going to give this a unique key, we can use the same one as before.

local pointsSave = 'points-'..plr.userId

We're also going to put the value of points inside a table, so it can be saved later on in the code.

local pointsTable = {plr.leaderstats.Points.Value}

We can now use a yielding function, known as SetAsync to set the value of our unique key from earlier, so we can save the value, we're going to use our unique key, and the table we created as the parameters.

DSService:SetAsync(pointsSave, pointsTable)

And we're done!

I'll put the full code below, but it's better for you to read this, and learn from it, as it will help you in the future!

Make sure to visit here if you'd like to learn more about DataStore, or leave a message if you have any comments or questions.

Full Code:

local DSService = game:GetService('DataStoreService'):GetDataStore('Points')

game.Players.PlayerAdded:Connect(function(plr)
    local leaderstats = plr:WaitForChild('leaderstats')

    local pointsSave = 'points-'..plr.userId
    local savePointsValue = leaderstats.Points

    local getSavedPoints = DSService:GetAsync(pointsSave)
    if getSavedPoints then
        savePointsValue.Value = getSavedPoints[1]
    else
        local pointsForSaving = {savePointsValue.Value}
        DSService:SetAsync(pointsSave, pointsForSaving)
    end
end)

game.Players.PlayerRemoving:Connect(function(plr)   
    local pointsSave = 'points-'..plr.userId
    local pointsTable = {plr.leaderstats.Points.Value}

    DSService:SetAsync(pointsSave, pointsTable)
end)

Note: Ensure you have Studio Access to API Services enabled in the Configure Game page of your place, else this won't work.

If I managed to answer your question, then be sure to accept my answer; it's greatly appreciated! :)

Happy scripting!

Ad

Answer this question