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
1 | local datastore = game:GetService( "DataStoreService" ) local ds 1 = datastore:GetDataStore( "GemSaveSystem" ) local ds 2 = 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 = ds 1 :GetAsync(plr.UserId) or 0 ds 1 :SetAsync(plr.UserId, gems.Value) cash.Value = ds 2 :GetAsync(plr.UserId) or 0 ds 2 :SetAsync(plr.UserId, cash.Value) gems.Changed:connect( function () ds 1 :SetAsync(plr.UserId, gems.Value) end ) cash.Changed:connect( function () ds 2 :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:
01 | local dataStoreService = game:GetService( "DataStoreService" ) --Service for DataStore |
02 | local cashDataStore = dataStoreService:GetDataStore( "cashDataStore" ) --DataStore for Cash |
03 |
04 | game.Players.PlayerAdded:Connect( function (player) |
05 | local leaderstats = Instance.new( "Folder" ,player) |
06 | leaderstats.Name = "leaderstats" |
07 |
08 | local cash = Instance.new( "IntValue" ,leaderstats) |
09 | cash.Name = "Cash" |
10 |
11 | local data -- keep nil for now |
12 | local success,errormessage = pcall ( function () --Message for success or error |
13 | data = cashDataStore:GetAsync(player.UserId.. "-Cash" ) --Put "-Cash" in case you want to add more values and to know what value |
14 | end ) |
15 | if success then --Checks if data is successfully loaded |
16 | cash.Value = data |
17 | else |
18 | print ( "There was an error while getting data" ) |
19 | warn(errormessage) --Warn is like print, but in yellow |
20 | end |
21 | end ) |
22 | game.Players.PlayerRemoving:Connect( function (player) -- When player is removed do |
23 | local success,errormessage = pcall ( function () |
24 | cashDataStore:SetAsync(player.UserId.. "-Cash" ,player.leaderstats.Cash.Value) |
25 | end ) |
26 | if success then |
27 | print ( "Data successfully saved!" ) |
28 | else |
29 | print ( "There was an error when saving data!" ) |
30 | warn(errormessage) |
31 | end |
32 | 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!
001 | local DataStoreService = game:GetService( "DataStoreService" ) |
002 |
003 | local OrbDataStore = DataStoreService:GetDataStore( "OrbDataStore" ) |
004 |
005 | local TicketDataStore = DataStoreService:GetDataStore( "TicketDataStore" ) |
006 |
007 | |
008 |
009 | game.Players.PlayerAdded:Connect( function (player) |
010 |
011 | local leaderstats = Instance.new( "Folder" ) |
012 |
013 | leaderstats.Name = "leaderstats" |
014 |
015 | leaderstats.Parent = player |
016 |
017 | local orbs = Instance.new( "IntValue" ) |
018 |
019 | orbs.Name = "Orbs" |
020 |
021 | orbs.Parent = leaderstats |
022 |
023 | local tickets = Instance.new( "IntValue" ) |
024 |
025 | tickets.Name = "Tickets" |
026 |
027 | tickets.Parent = leaderstats |
028 |
029 | local data |
030 |
031 | local success, errormessage = pcall ( function () |
032 |
033 | data = OrbDataStore:GetAsync(player.UserId.. "orbs" ) |
034 |
035 | end ) |
036 |
037 | if success then |
038 |
039 | orbs.Value = data |
040 |
041 | else |
042 |
043 | print ( "There was an error while getting your Orb data" ) |
044 |
045 | warn(errormessage) |
046 |
047 | end |
048 |
049 | local success, errormessage = pcall ( function () |
050 |
051 | data = TicketDataStore:GetAsync(player.UserId.. "tickets" ) |
052 |
053 | end ) |
054 |
055 | if success then |
056 |
057 | tickets.Value = data |
058 |
059 | else |
060 |
061 | print ( "There was an error while grabbing your Ticket data" ) |
062 |
063 | warn(errormessage) |
064 |
065 | end |
066 |
067 | end ) |
068 |
069 | |
070 |
071 | game.Players.PlayerRemoving:Connect( function (player) |
072 |
073 | local success, errormessage = pcall ( function () |
074 |
075 | OrbDataStore:SetAsync(player.UserId.. "orbs" ,player.leaderstats.Orbs.Value) |
076 |
077 | end ) |
078 |
079 | if success then |
080 |
081 | print ( "Orb Data Successfully Saved" ) |
082 |
083 | else |
084 |
085 | print ( "There was an error while saving Orb data" ) |
086 |
087 | warn(errormessage) |
088 |
089 | end |
090 |
091 | local success, errormessage = pcall ( function () |
092 |
093 | TicketDataStore:SetAsync(player.UserId.. "tickets" ,player.leaderstats.Tickets.Value) |
094 |
095 | end ) |
096 |
097 | if success then |
098 |
099 | print ( "Ticket Data Successfully Saved" ) |
100 |
101 | else |
102 |
103 | print ( "There was an error while saving Ticket data" ) |
104 |
105 | warn(errormessage) |
106 |
107 | end |
108 |
109 | end ) |