Advertisement
XiByteGam123457890

Advanced Customizable Leaderstat

Jan 19th, 2025
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.15 KB | Source Code | 0 0
  1. Advanced Customizable Leader stat
  2. -------------------------------------------------------------------------
  3. -- Get services and configure the datastore
  4. local DataStoreService = game:GetService("DataStoreService")
  5. local config = script:WaitForChild("datastore_config")
  6. local myDataStore = DataStoreService:GetDataStore("$Data$!"..config:FindFirstChild("DataVersion").Value)
  7.  
  8. -- Settings from the configuration script
  9. local saving = config:FindFirstChild("Saving")
  10. local autoSave = config:FindFirstChild("AutoSave")
  11.  
  12. -- Function to create a table of player stats
  13. local function create_table(plr)
  14.     local player_stats = {}
  15.  
  16.     for _, folder in pairs(script:FindFirstChild("Plr"):GetChildren()) do
  17.         if folder:IsA("Folder") then
  18.             print(folder)
  19.             for _, stat in pairs(plr:FindFirstChild(folder.Name):GetChildren()) do
  20.                 player_stats[stat.Name.." "..folder.Name] = stat.Value
  21.             end
  22.         end
  23.     end
  24.  
  25.     return player_stats
  26. end
  27.  
  28. -- Function to save player data
  29. local function saveData(plr)
  30.     local player_stats = create_table(plr)
  31.  
  32.     local success, err = pcall(function()
  33.         local key = plr.UserId.."'s' Data"
  34.         myDataStore:SetAsync(key, player_stats)
  35.     end)
  36.  
  37.     if success then
  38.         print("Saved Data Correctly!")
  39.     else
  40.         warn(err)
  41.     end
  42. end
  43.  
  44. -- Event: When a player joins the game
  45. game.Players.PlayerAdded:Connect(function(plr)
  46.     local key = plr.UserId.."'s' Data"
  47.     local data = myDataStore:GetAsync(key)
  48.  
  49.     print(data)
  50.  
  51.     -- Iterate through folders to clone and set up player stats
  52.     for _, folder in pairs(script:FindFirstChild("Plr"):GetChildren()) do
  53.         if folder:IsA("Folder") then
  54.             local fc = folder:Clone()
  55.             fc.Parent = plr
  56.  
  57.             if saving.Value == true then
  58.                 for _, item in pairs(fc:GetChildren()) do
  59.                     if data then
  60.                         item.Value = data[item.Name.." "..folder.Name]
  61.                     else
  62.                         warn("There is no data!")
  63.                     end
  64.                 end
  65.             end
  66.         end
  67.  
  68.         -- Auto-save loop
  69.         while autoSave.Value > 0 do
  70.             task.wait(autoSave.Value * 60)
  71.             saveData(plr)
  72.             print("Saved", data)
  73.         end
  74.     end
  75. end)
  76.  
  77. -- Event: When a player leaves the game
  78. game.Players.PlayerRemoving:Connect(function(plr)
  79.     if saving.Value == true then
  80.         saveData(plr)
  81.     end
  82. end)
  83.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement