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.
1 | local dataStoreService = game:GetService( "DataStoreService" ) |
2 | local strengthDataStore = dataStoreService:GetDataStore( "StrengthDataStore" ) |
3 | local cashDataStore = dataStoreService:GetDataStore( "CashDataStore" ) |
After doing so, we can add the rest of the code for your leaderboard.
01 | local dataStoreService = game:GetService( "DataStoreService" ) |
02 | local strengthDataStore = dataStoreService:GetDataStore( "StrengthDataStore" ) |
03 | local cashDataStore = dataStoreService:GetDataStore( "CashDataStore" ) |
05 | game.Players.PlayerAdded:Connect( function (player) |
06 | local stats = Instance.new( "Folder" ) |
07 | stats.Name = "leaderstats" |
10 | local strength = Instance.new( "IntValue" ) |
11 | strength.Name = "Strength" |
13 | strength.Parent = stats |
15 | local cash = Instance.new( "IntValue" ) |
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:
01 | local dataStoreService = game:GetService( "DataStoreService" ) |
02 | local strengthDataStore = dataStoreService:GetDataStore( "StrengthDataStore" ) |
03 | local cashDataStore = dataStoreService:GetDataStore( "CashDataStore" ) |
05 | game.Players.PlayerAdded:Connect( function (player) |
06 | local stats = Instance.new( "Folder" ) |
07 | stats.Name = "leaderstats" |
10 | local strength = Instance.new( "IntValue" ) |
11 | strength.Name = "Strength" |
12 | strength.Value = strengthDataStore:GetAsync(player.UserId) or 0 |
13 | strength.Parent = stats |
15 | local cash = Instance.new( "IntValue" ) |
17 | cash.Value = cashDataStore:GetAsync(player.UserId) or 0 |
20 | strength.Changed:Connect( function () |
21 | strengthDataStore:SetAsync(player.UserId, strength.Value) |
23 | cash.Changed:Connect( function () |
24 | cashDataStore:SetAsync(player.UserId, cash.Value) |
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.
01 | local dataStoreService = game:GetService( "DataStoreService" ) |
02 | local strengthDataStore = dataStoreService:GetDataStore( "StrengthDataStore" ) |
03 | local cashDataStore = dataStoreService:GetDataStore( "CashDataStore" ) |
05 | game.Players.PlayerAdded:Connect( function (player) |
06 | local stats = Instance.new( "Folder" ) |
07 | stats.Name = "leaderstats" |
10 | local strength = Instance.new( "IntValue" ) |
11 | strength.Name = "Strength" |
12 | strength.Value = strengthDataStore:GetAsync(player.UserId) or 0 |
13 | strength.Parent = stats |
15 | local cash = Instance.new( "IntValue" ) |
17 | cash.Value = cashDataStore:GetAsync(player.UserId) or 0 |
20 | strength.Changed:Connect( function () |
21 | strengthDataStore:SetAsync(player.UserId, strength.Value) |
23 | cash.Changed:Connect( function () |
24 | cashDataStore:SetAsync(player.UserId, cash.Value) |
28 | game.Players.PlayerRemoving:Connect( function (player) |
29 | cashDataStore:SetAsync(player.UserId, player.leaderstats.Cash.Value) |
30 | strengthDataStore:SetAsync(player.UserId, player.leaderstats.Strength.Value) |
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.