[DATA SAVING] Arrests and Arrested both save, but not the team that the player was on?
game.Players.PlayerAdded:Connect(function(plr)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = plr
local arrests = Instance.new("IntValue")
arrests.Value = 0
arrests.Name = "Arrests"
arrests.Parent = leaderstats
local arrested = Instance.new("IntValue")
arrested.Value = 0
arrested.Name = "Arrested"
arrested.Parent = leaderstats
local playerUserId = "Player_"..plr.UserId
local data = playerData:GetAsync(playerUserId)
if data then
arrests.Value = data["Arrests"]
arrested.Value = data["Arrested"]
plr.Team = data["Team"]
else
arrests.Value = 0
arrested.Value = 0
end
end)
local function createTable(player)
local playerStats = {}
for i,stat in pairs(player.leaderstats:GetChildren()) do
playerStats[stat.Name] = stat.Value
end
playerStats["Team"] = player.Team
return playerStats
end
local function onPlayerExit(player)
local playerStats = createTable(player)
local success, errorMessage = pcall(function()
local playerUserId = "Player_"..player.UserId
playerData:SetAsync(playerUserId,playerStats)
end)
if not success then
warn("error when saving data")
end
end
game.Players.PlayerRemoving:Connect(onPlayerExit)