I know how to create a leaderboard but I do not know how to save it when a player leaves the game and load when the player rejoins. I am trying to do tutorials but all five of them did not work for me. All I ask is that someone who knows this stuff gives me an answer on how to make a saving and loading leaderboard
my code is down here and this is the tutorial I used for it: https://www.youtube.com/watch?v=lS20iM7iRQE
local datastore = game:GetService("DataStoreService") local ds1 = datastore:GetDataStore("GemSaveSystem") local ds2 = datastore:GetDataStore("CashSaveSystem") game.Players.PlayerAdded:connect(function(plr) local folder = Instance.new("Folder", plr) folder.Name = "leaderstats" local gems = Instance.new("IntValue", folder) gems.Name = "Gems" local cash = Instance.new("IntValue", folder) cash.Name = "Cash" gems.Value = ds1:GetAsync(plr.UserId) or 0 ds1:SetAsync(plr.UserId, gems.Value) cash.Value = ds2:GetAsync(plr.UserId) or 0 ds2:SetAsync(plr.UserId, cash.Value) gems.Changed:connect(function() ds1:SetAsync(plr.UserId, gems.Value) end) cash.Changed:connect(function() ds2:SetAsync(plr.UserId, cash.Value) end) end)
here is more info about DataStore: https://developer.roblox.com/articles/Data-store
Take Note:
Accept my answer if it works for you!
Edited Script:
local dataStoreService = game:GetService("DataStoreService") --Service for DataStore local cashDataStore = dataStoreService:GetDataStore("cashDataStore")--DataStore for Cash game.Players.PlayerAdded:Connect(function(player) local leaderstats = Instance.new("Folder",player) leaderstats.Name = "leaderstats" local cash = Instance.new("IntValue",leaderstats) cash.Name = "Cash" local data -- keep nil for now local success,errormessage = pcall(function() --Message for success or error data = cashDataStore:GetAsync(player.UserId.."-Cash") --Put "-Cash" in case you want to add more values and to know what value end) if success then --Checks if data is successfully loaded cash.Value = data else print("There was an error while getting data") warn(errormessage) --Warn is like print, but in yellow end end) game.Players.PlayerRemoving:Connect(function(player) -- When player is removed do local success,errormessage = pcall(function() cashDataStore:SetAsync(player.UserId.."-Cash",player.leaderstats.Cash.Value) end) if success then print("Data successfully saved!") else print("There was an error when saving data!") warn(errormessage) end end)
Hi, this is the script. I hope you don't just copy and paste. Just to make it easier for you to understand, I added some comments. I understand that you're a beginner so just to let you under stand more, here are some more links. Read up! Click me! Click me! Click me! Click me! Click me!
What I would suggest is adding on a pcall that ensures that if data fails to save or load that you will know in the developer log and you could also let the player know as well. This is the code I use to save my player data, i could of made it more efficient with the parenting yes, but you get the idea. This should work, try it out. Hope this helps!
local DataStoreService = game:GetService("DataStoreService") local OrbDataStore = DataStoreService:GetDataStore("OrbDataStore") local TicketDataStore = DataStoreService:GetDataStore("TicketDataStore") game.Players.PlayerAdded:Connect(function(player) local leaderstats = Instance.new("Folder") leaderstats.Name = "leaderstats" leaderstats.Parent = player local orbs = Instance.new("IntValue") orbs.Name = "Orbs" orbs.Parent = leaderstats local tickets = Instance.new("IntValue") tickets.Name = "Tickets" tickets.Parent = leaderstats local data local success, errormessage = pcall(function() data = OrbDataStore:GetAsync(player.UserId.."orbs") end) if success then orbs.Value = data else print("There was an error while getting your Orb data") warn(errormessage) end local success, errormessage = pcall(function() data = TicketDataStore:GetAsync(player.UserId.."tickets") end) if success then tickets.Value = data else print("There was an error while grabbing your Ticket data") warn(errormessage) end end) game.Players.PlayerRemoving:Connect(function(player) local success, errormessage = pcall(function() OrbDataStore:SetAsync(player.UserId.."orbs",player.leaderstats.Orbs.Value) end) if success then print("Orb Data Successfully Saved") else print("There was an error while saving Orb data") warn(errormessage) end local success, errormessage = pcall(function() TicketDataStore:SetAsync(player.UserId.."tickets",player.leaderstats.Tickets.Value) end) if success then print("Ticket Data Successfully Saved") else print("There was an error while saving Ticket data") warn(errormessage) end end)