Cakey3101

Legends Of Speed - 8 - Data Script

Sep 19th, 2025
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.74 KB | Source Code | 0 0
  1. local Players = game:GetService("Players")
  2. local DataStoreService = game:GetService("DataStoreService")
  3.  
  4. local DataStore = DataStoreService:GetDataStore("_0")
  5.  
  6. local function OnPlayerJoin(Player)
  7.     local UserId = Player.UserId
  8.     local Key = "Player_" .. UserId
  9.  
  10.     local Leaderstats = Instance.new("Folder")
  11.     Leaderstats.Name = "leaderstats"
  12.     Leaderstats.Parent = Player
  13.  
  14.     local RedeemedCodes = Instance.new("Folder")
  15.     RedeemedCodes.Name = "RedeemedCodes"
  16.     RedeemedCodes.Parent = Player
  17.  
  18.     local Gems = Instance.new("IntValue")
  19.     Gems.Name = "Gems"
  20.     Gems.Value = 0
  21.     Gems.Parent = Player
  22.  
  23.     local XP = Instance.new("IntValue")
  24.     XP.Name = "XP"
  25.     XP.Value = 0
  26.     XP.Parent = Player
  27.  
  28.     local RequiredXP = Instance.new("IntValue")
  29.     RequiredXP.Name = "RequiredXP"
  30.     RequiredXP.Value = 0
  31.     RequiredXP.Parent = Player
  32.  
  33.     local Level = Instance.new("IntValue")
  34.     Level.Name = "Level"
  35.     Level.Value = 1
  36.     Level.Parent = Player
  37.  
  38.     local Steps = Instance.new("IntValue")
  39.     Steps.Name = "Steps"
  40.     Steps.Value = 0
  41.     Steps.Parent = Leaderstats
  42.  
  43.     local Rebirths = Instance.new("IntValue")
  44.     Rebirths.Name = "Rebirths"
  45.     Rebirths.Value = 0
  46.     Rebirths.Parent = Leaderstats
  47.  
  48.     local Races = Instance.new("IntValue")
  49.     Races.Name = "Races"
  50.     Races.Value = 0
  51.     Races.Parent = Leaderstats
  52.  
  53.     local Hoops = Instance.new("IntValue")
  54.     Hoops.Name = "Hoops"
  55.     Hoops.Value = 0
  56.     Hoops.Parent = Leaderstats
  57.  
  58.     local Success, SavedData = pcall(function()
  59.         return DataStore:GetAsync(Key)
  60.     end)
  61.  
  62.     if Success and SavedData then
  63.         Gems.Value = SavedData.Gems or 0
  64.         XP.Value = SavedData.XP or 0
  65.         RequiredXP.Value = SavedData.RequiredXP or 0
  66.         Level.Value = SavedData.Level or 1
  67.         Steps.Value = SavedData.Steps or 0
  68.         Rebirths.Value = SavedData.Rebirths or 0
  69.         Races.Value = SavedData.Races or 0
  70.         Hoops.Value = SavedData.Hoops or 0
  71.  
  72.         if SavedData.RedeemedCodes then
  73.             for _, Code in ipairs(SavedData.RedeemedCodes) do
  74.                 local CodeInstance = Instance.new("StringValue")
  75.                 CodeInstance.Name = Code
  76.                 CodeInstance.Parent = RedeemedCodes
  77.             end
  78.         end
  79.     else
  80.         warn("Failed To Load Data For", Player.Name)
  81.     end
  82. end
  83.  
  84. local function OnPlayerLeave(Player)
  85.     local UserId = Player.UserId
  86.     local Key = "Player_" .. UserId
  87.  
  88.     local Leaderstats = Player:FindFirstChild("leaderstats")
  89.     local RedeemedCodes = Player:FindFirstChild("RedeemedCodes")
  90.  
  91.     if Leaderstats and RedeemedCodes then
  92.         local Gems = Player:FindFirstChild("Gems")
  93.         local XP = Player:FindFirstChild("XP")
  94.         local RequiredXP = Player:FindFirstChild("RequiredXP")
  95.         local Level = Player:FindFirstChild("Level")
  96.  
  97.         local Steps = Leaderstats:FindFirstChild("Steps")
  98.         local Rebirths = Leaderstats:FindFirstChild("Rebirths")
  99.         local Races = Leaderstats:FindFirstChild("Races")
  100.         local Hoops = Leaderstats:FindFirstChild("Hoops")
  101.  
  102.         if Gems and XP and RequiredXP and Level and Steps and Rebirths and Races and Hoops then
  103.             local Data = {
  104.                 Gems = Gems.Value,
  105.                 XP = XP.Value,
  106.                 RequiredXP = RequiredXP.Value,
  107.                 Level = Level.Value,
  108.                 Steps = Steps.Value,
  109.                 Rebirths = Rebirths.Value,
  110.                 Races = Races.Value,
  111.                 Hoops = Hoops.Value,
  112.                 RedeemedCodes = {}
  113.             }
  114.  
  115.             for _, CodeInstance in ipairs(RedeemedCodes:GetChildren()) do
  116.                 if CodeInstance:IsA("StringValue") then
  117.                     table.insert(Data.RedeemedCodes, CodeInstance.Name)
  118.                 end
  119.             end
  120.  
  121.             local Success, Error = pcall(function()
  122.                 DataStore:SetAsync(Key, Data)
  123.             end)
  124.  
  125.             if not Success then
  126.                 warn("Failed To Save Data For", Player.Name, "Error:", Error)
  127.             end
  128.         end
  129.     end
  130. end
  131.  
  132. Players.PlayerAdded:Connect(OnPlayerJoin)
  133. Players.PlayerRemoving:Connect(OnPlayerLeave)
  134.  
  135. game:BindToClose(function()
  136.     for _, Player in pairs(Players:GetPlayers()) do
  137.         OnPlayerLeave(Player)
  138.         Player:Kick("The server has shutdown!")
  139.     end
  140. end)
Advertisement
Add Comment
Please, Sign In to add comment