Sungmingamerpro13

My New DataStore of STORY GAME

Aug 30th, 2026
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
CSS 2.74 KB | None | 0 0
  1. local Players = game:GetService("Players")
  2. local DataStoreService = game:GetService("DataStoreService")
  3.  
  4. local saveDataStore = DataStoreService:GetDataStore("PlayerData")
  5.  
  6.  
  7. Players.PlayerAdded:Connect(function(plr)
  8.  
  9.     --==================================================
  10.     -- LEADERSTATS
  11.     --==================================================
  12.  
  13.     local stats = Instance.new("Folder")
  14.     stats.Name = "leaderstats"
  15.     stats.Parent = plr
  16.  
  17.     local Wins = Instance.new("NumberValue")
  18.     Wins.Name = "Wins"
  19.     Wins.Value = 0
  20.     Wins.Parent = stats
  21.  
  22.     local Rounds = Instance.new("NumberValue")
  23.     Rounds.Name = "Rounds"
  24.     Rounds.Value = 0
  25.     Rounds.Parent = stats
  26.  
  27.  
  28.     --==================================================
  29.     -- CARGAR DATOS
  30.     --==================================================
  31.  
  32.     local success, data = pcall(function()
  33.         return saveDataStore:GetAsync(plr.UserId)
  34.     end)
  35.  
  36.     if success and data then
  37.  
  38.         if data.Stats then
  39.  
  40.             if data.Stats.Wins then
  41.                 Wins.Value = data.Stats.Wins
  42.             end
  43.  
  44.             if data.Stats.Rounds then
  45.                 Rounds.Value = data.Stats.Rounds
  46.             end
  47.  
  48.         end
  49.  
  50.     elseif not success then
  51.         warn("No se pudieron cargar los datos de " .. plr.Name)
  52.     end
  53.  
  54.  
  55.     --==================================================
  56.     -- TELEPORT DATA
  57.     --==================================================
  58.  
  59.     local joinData = plr:GetJoinData()
  60.     local teleportData = joinData.TeleportData
  61.  
  62.     if teleportData then
  63.  
  64.         -- Si terminó una ronda, aumentar Rounds
  65.         if teleportData.AddRounds == true then
  66.             Rounds.Value += 1
  67.         end
  68.  
  69.         -- Si ganó, aumentar Wins
  70.         if teleportData.AddWins == true then
  71.             Wins.Value += 1
  72.         end
  73.  
  74.     end
  75.  
  76. end)
  77.  
  78.  
  79. --==================================================
  80. -- GUARDAR AL SALIR
  81. --==================================================
  82.  
  83. Players.PlayerRemoving:Connect(function(plr)
  84.  
  85.     local stats = plr:FindFirstChild("leaderstats")
  86.  
  87.     if not stats then
  88.         return
  89.     end
  90.  
  91.     local saveData = {
  92.         Stats = {
  93.             Wins = stats.Wins.Value,
  94.             Rounds = stats.Rounds.Value
  95.         }
  96.     }
  97.  
  98.     local success, errorMessage = pcall(function()
  99.         saveDataStore:SetAsync(plr.UserId, saveData)
  100.     end)
  101.  
  102.     if not success then
  103.         warn("Error guardando datos de " .. plr.Name .. ": " .. errorMessage)
  104.     end
  105.  
  106. end)
  107.  
  108.  
  109. --==================================================
  110. -- GUARDAR CUANDO CIERRA EL SERVIDOR
  111. --==================================================
  112.  
  113. game:BindToClose(function()
  114.  
  115.     for _, plr in pairs(Players:GetPlayers()) do
  116.  
  117.         local stats = plr:FindFirstChild("leaderstats")
  118.  
  119.         if stats then
  120.  
  121.             local saveData = {
  122.                 Stats = {
  123.                     Wins = stats.Wins.Value,
  124.                     Rounds = stats.Rounds.Value
  125.                 }
  126.             }
  127.  
  128.             pcall(function()
  129.                 saveDataStore:SetAsync(plr.UserId, saveData)
  130.             end)
  131.  
  132.         end
  133.  
  134.     end
  135.  
  136.     task.wait(2)
  137.  
  138. end)
  139.  
Advertisement
Add Comment
Please, Sign In to add comment