Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local Players = game:GetService("Players")
- local DataStoreService = game:GetService("DataStoreService")
- local saveDataStore = DataStoreService:GetDataStore("PlayerData")
- Players.PlayerAdded:Connect(function(plr)
- --==================================================
- -- LEADERSTATS
- --==================================================
- local stats = Instance.new("Folder")
- stats.Name = "leaderstats"
- stats.Parent = plr
- local Wins = Instance.new("NumberValue")
- Wins.Name = "Wins"
- Wins.Value = 0
- Wins.Parent = stats
- local Rounds = Instance.new("NumberValue")
- Rounds.Name = "Rounds"
- Rounds.Value = 0
- Rounds.Parent = stats
- --==================================================
- -- CARGAR DATOS
- --==================================================
- local success, data = pcall(function()
- return saveDataStore:GetAsync(plr.UserId)
- end)
- if success and data then
- if data.Stats then
- if data.Stats.Wins then
- Wins.Value = data.Stats.Wins
- end
- if data.Stats.Rounds then
- Rounds.Value = data.Stats.Rounds
- end
- end
- elseif not success then
- warn("No se pudieron cargar los datos de " .. plr.Name)
- end
- --==================================================
- -- TELEPORT DATA
- --==================================================
- local joinData = plr:GetJoinData()
- local teleportData = joinData.TeleportData
- if teleportData then
- -- Si terminó una ronda, aumentar Rounds
- if teleportData.AddRounds == true then
- Rounds.Value += 1
- end
- -- Si ganó, aumentar Wins
- if teleportData.AddWins == true then
- Wins.Value += 1
- end
- end
- end)
- --==================================================
- -- GUARDAR AL SALIR
- --==================================================
- Players.PlayerRemoving:Connect(function(plr)
- local stats = plr:FindFirstChild("leaderstats")
- if not stats then
- return
- end
- local saveData = {
- Stats = {
- Wins = stats.Wins.Value,
- Rounds = stats.Rounds.Value
- }
- }
- local success, errorMessage = pcall(function()
- saveDataStore:SetAsync(plr.UserId, saveData)
- end)
- if not success then
- warn("Error guardando datos de " .. plr.Name .. ": " .. errorMessage)
- end
- end)
- --==================================================
- -- GUARDAR CUANDO CIERRA EL SERVIDOR
- --==================================================
- game:BindToClose(function()
- for _, plr in pairs(Players:GetPlayers()) do
- local stats = plr:FindFirstChild("leaderstats")
- if stats then
- local saveData = {
- Stats = {
- Wins = stats.Wins.Value,
- Rounds = stats.Rounds.Value
- }
- }
- pcall(function()
- saveDataStore:SetAsync(plr.UserId, saveData)
- end)
- end
- end
- task.wait(2)
- end)
Advertisement
Add Comment
Please, Sign In to add comment