My code is:
game.Players.PlayerAdded:Connect(function(player) local stats = Instance.new("Folder") stats.Name = "leaderstats" stats.Parent = player local strength = Instance.new("IntValue") strength.Name = "Strength" strength.Value = 0 strength.Parent = stats local cash = Instance.new("IntValue") cash.Name = "Cash" cash.Value = 0 cash.Parent = stats end)
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.
local dataStoreService = game:GetService("DataStoreService") local strengthDataStore = dataStoreService:GetDataStore("StrengthDataStore") local cashDataStore = dataStoreService:GetDataStore("CashDataStore")
After doing so, we can add the rest of the code for your leaderboard.
local dataStoreService = game:GetService("DataStoreService") local strengthDataStore = dataStoreService:GetDataStore("StrengthDataStore") local cashDataStore = dataStoreService:GetDataStore("CashDataStore") game.Players.PlayerAdded:Connect(function(player) local stats = Instance.new("Folder") stats.Name = "leaderstats" stats.Parent = player local strength = Instance.new("IntValue") strength.Name = "Strength" strength.Value = 0 strength.Parent = stats local cash = Instance.new("IntValue") cash.Name = "Cash" cash.Value = 0 cash.Parent = stats end)
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:
local dataStoreService = game:GetService("DataStoreService") local strengthDataStore = dataStoreService:GetDataStore("StrengthDataStore") local cashDataStore = dataStoreService:GetDataStore("CashDataStore") game.Players.PlayerAdded:Connect(function(player) local stats = Instance.new("Folder") stats.Name = "leaderstats" stats.Parent = player local strength = Instance.new("IntValue") strength.Name = "Strength" strength.Value = strengthDataStore:GetAsync(player.UserId) or 0 strength.Parent = stats local cash = Instance.new("IntValue") cash.Name = "Cash" cash.Value = cashDataStore:GetAsync(player.UserId) or 0 cash.Parent = stats strength.Changed:Connect(function() strengthDataStore:SetAsync(player.UserId, strength.Value) end) cash.Changed:Connect(function() cashDataStore:SetAsync(player.UserId, cash.Value) end) end)
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.
local dataStoreService = game:GetService("DataStoreService") local strengthDataStore = dataStoreService:GetDataStore("StrengthDataStore") local cashDataStore = dataStoreService:GetDataStore("CashDataStore") game.Players.PlayerAdded:Connect(function(player) local stats = Instance.new("Folder") stats.Name = "leaderstats" stats.Parent = player local strength = Instance.new("IntValue") strength.Name = "Strength" strength.Value = strengthDataStore:GetAsync(player.UserId) or 0 strength.Parent = stats local cash = Instance.new("IntValue") cash.Name = "Cash" cash.Value = cashDataStore:GetAsync(player.UserId) or 0 cash.Parent = stats strength.Changed:Connect(function() strengthDataStore:SetAsync(player.UserId, strength.Value) end) cash.Changed:Connect(function() cashDataStore:SetAsync(player.UserId, cash.Value) end) end) game.Players.PlayerRemoving:Connect(function(player) cashDataStore:SetAsync(player.UserId, player.leaderstats.Cash.Value) strengthDataStore:SetAsync(player.UserId, player.leaderstats.Strength.Value) end)
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.
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()