Advertisement
OnFireRobloxScriptin

Datastore with leaderstats script

Aug 31st, 2024
1,915
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.75 KB | None | 0 0
  1. --//Datastore
  2. local DataStoreService = game:GetService("DataStoreService") --Gets the DataStoreService
  3. local dataStore = DataStoreService:GetDataStore("Test") --Gives the Datastore a name
  4.  
  5. --//Function
  6. local function saveData(player) --Create out own function for saving data
  7.     local tableToSave = { --Creates a table for our values
  8.         player.leaderstats.Points.Value; --Saves the first value
  9.         player.leaderstats.Wins.Value; --Saves the second value
  10.     } --You can continue the list in the same order by adding "player.leaderstats.IntValueName.Value;"
  11.  
  12.     local success, errorMessage = pcall(dataStore.SetAsync, dataStore, player.UserId, tableToSave) --Checks if datastore succeeded
  13.  
  14.     if success then --If datastore success then
  15.         print("Data has been saved!") --Prints success
  16.     else --If datastore fails then
  17.         print("Data has not been saved!") --Prints failure
  18.     end
  19. end
  20.  
  21. --//leaderstats
  22. game.Players.PlayerAdded:Connect(function(player) --When a player joins the game
  23.     player.CharacterAdded:Wait() --Wait for the player to load
  24.     local leaderstats = Instance.new("Folder") --Creates a new folder for the player
  25.     leaderstats.Name = "leaderstats" --Sets Folder name to "leaderstats" --MAKE SURE YOU DON'T CHANGE THIS
  26.     leaderstats.Parent = player --Puts the Folder under the player
  27.  
  28.     local points = Instance.new("IntValue") --Creates an IntValue
  29.     points.Name = "Points" --Sets IntValue name to "Points" (If you change this make sure you change all of them)
  30.     points.Parent = leaderstats --Puts the IntValue under the "leaderstats" folder
  31.     points.Value = 0 --Gives the IntValue Value to start off with
  32.  
  33.     local wins = Instance.new("IntValue") --Creates an IntValue
  34.     wins.Name = "Wins" --Sets IntValue name to "Wins" (If you change this make sure you change all of them)
  35.     wins.Parent = leaderstats --Puts the IntValue under the "leaderstats" folder
  36.     wins.Value = 0 --Gives the IntValue Value to start off with
  37.  
  38.     local data = nil --Data is empty
  39.  
  40.     local success, errorMessage = pcall(function()
  41.         data = dataStore:GetAsync(player.UserId) --Finds the player's UserId and data
  42.     end)
  43.  
  44.     if success and data then --If UserId and Data found then
  45.         points.Value = data[1] --Sets points to the first set of data
  46.         wins.Value = data[2] --Sets wins to the first set of data
  47.     else --If UserId or Data not found then
  48.         print("The Player has no Data!") --Player has no data
  49.         warn(errorMessage)
  50.     end
  51. end)
  52.  
  53. game.Players.PlayerRemoving:Connect(function(player) --When player is leaving the game
  54.     saveData(player) --Save the player's data
  55. end)
  56.  
  57. game:BindToClose(function() --When the game's servers are shutting down
  58.     for _, player in ipairs(game.Players:GetPlayers()) do --loop through all the players in the server
  59.         task.spawn(saveData, player) --save all the player's data
  60.     end
  61. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement