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

How to make saving leaderboard stats?

Asked by
jbm_pl 4
4 years ago

My code is:

01game.Players.PlayerAdded:Connect(function(player)
02    local stats = Instance.new("Folder")
03    stats.Name = "leaderstats"
04    stats.Parent = player
05 
06    local strength = Instance.new("IntValue")
07    strength.Name = "Strength"
08    strength.Value = 0
09    strength.Parent = stats
10 
11    local cash = Instance.new("IntValue")
12    cash.Name = "Cash"
13    cash.Value = 0
14    cash.Parent = stats
15 
16end)

2 answers

Log in to vote
0
Answered by 4 years ago

First you need a data store. That is where you can store the values of these and save them. Make sure this script is in the workspace and is a regular server script.

1local dataStoreService = game:GetService("DataStoreService")
2local strengthDataStore = dataStoreService:GetDataStore("StrengthDataStore")
3local cashDataStore = dataStoreService:GetDataStore("CashDataStore")

After doing so, we can add the rest of the code for your leaderboard.

01local dataStoreService = game:GetService("DataStoreService")
02local strengthDataStore = dataStoreService:GetDataStore("StrengthDataStore")
03local cashDataStore = dataStoreService:GetDataStore("CashDataStore")
04 
05game.Players.PlayerAdded:Connect(function(player)
06    local stats = Instance.new("Folder")
07    stats.Name = "leaderstats"
08    stats.Parent = player
09 
10    local strength = Instance.new("IntValue")
11    strength.Name = "Strength"
12    strength.Value = 0
13    strength.Parent = stats
14 
15    local cash = Instance.new("IntValue")
16    cash.Name = "Cash"
17    cash.Value = 0
18    cash.Parent = stats
19 
20end)

Right now this isn't doing much as we only have the data stores but we are not saving the data. Do so with this:

01local dataStoreService = game:GetService("DataStoreService")
02local strengthDataStore = dataStoreService:GetDataStore("StrengthDataStore")
03local cashDataStore = dataStoreService:GetDataStore("CashDataStore")
04 
05game.Players.PlayerAdded:Connect(function(player)
06    local stats = Instance.new("Folder")
07    stats.Name = "leaderstats"
08    stats.Parent = player
09 
10    local strength = Instance.new("IntValue")
11    strength.Name = "Strength"
12    strength.Value = strengthDataStore:GetAsync(player.UserId) or 0
13    strength.Parent = stats
14 
15    local cash = Instance.new("IntValue")
View all 26 lines...

Just remember: GetAsync() is getting data, and SetAsync() is updating it. Now we need to save when they leave as the data may not have been saved correctly. You can never be too sure.

01local dataStoreService = game:GetService("DataStoreService")
02local strengthDataStore = dataStoreService:GetDataStore("StrengthDataStore")
03local cashDataStore = dataStoreService:GetDataStore("CashDataStore")
04 
05game.Players.PlayerAdded:Connect(function(player)
06    local stats = Instance.new("Folder")
07    stats.Name = "leaderstats"
08    stats.Parent = player
09 
10    local strength = Instance.new("IntValue")
11    strength.Name = "Strength"
12    strength.Value = strengthDataStore:GetAsync(player.UserId) or 0
13    strength.Parent = stats
14 
15    local cash = Instance.new("IntValue")
View all 31 lines...

Now make sure the game is published and "Studio Access to API Services" is true, as the game has to be able to save to the data store service, only allowed to do so if this option is true. Studio Access to HTTP services is always false when using a data store.

0
It works. Thanks! jbm_pl 4 — 4y
Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

DataStoreService. DataStores are basically a way to save your data. Alvinblox and Devking both made videos about it, However, I recommend the video from devking (Which I'll link below). Another thing I should mention about these videos is that they do not include game:BindToClose and I'll put it in the link below as well. This is a starting point, and not a full answer. Devking's DataStore Video DataModel:BindToClose()

Answer this question