just save the name of the team inside of a datastore when leaving and call it back when joined.
local dss = game:GetService("DataStoreService")
local ds = dss:GetDataStore("Team")
game.Players.PlayerAdded:Connect(function(plr)
local team = ds:GetAsync(plr.UserId) --get the team on join
if team then --checks if team is nil or not
plr.Team = game.Teams[team] -- set their team to that team
end
end)
function save(plr)
if plr.Team then
ds:SetAsync(plr.UserId, plr.Team.Name) --save the team name into a datastore
end
end
game.Players.PlayerRemoving(save) --save when player leaves
game:BindToClose(function()
for i,v in pairs(game.Players:GetPlayers()) do --go through all of the players
save(v) --call the save function
end
end)
How you plan to customize the teams in the feature
Assuming the datastore saves player data as a dictionary, you can just save their team as data.Team = Player.Team.Name assuming the teams name remain constant. Then when the player loads into the game, you retrieve the data, and if their data and the Team key exists, set Player.Team to game.Teams:FindFirstChild(data.Team).
My friend made the datastore because IDK how datastores work so here is the datastore script:
local DataStore = game:GetService("DataStoreService")
local DeathStore = DataStore:GetDataStore("Deaths")
local RCStore = DataStore:GetDataStore("RC")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Model", player)
leaderstats.Name = "leaderstats"
local Deaths = Instance.new("IntValue", leaderstats)
Deaths.Name = "Deaths"
local RC = Instance.new("IntValue", leaderstats)
RC.Name = "RC"
pcall(function()
local DeathValue = DeathStore:GetAsync(player.UserId)
local RCValue = RCStore:GetAsync(player.UserId)
player.CharacterAdded:Connect(function(character)
character:WaitForChild("Humanoid").Died:Connect(function()
Deaths.Value = Deaths.Value + 1
end)
end)
if DeathValue then
Deaths.Value = DeathValue
else
Deaths.Value = 0
end
if RCValue then
RC.Value = RCValue
else
RC.Value = 0
end
end)
end)
game.Players.PlayerRemoving:Connect(function(player)
pcall(function()
if player:FindFirstChild("leaderstats") then
if player.leaderstats:FindFirstChild("Deaths") and player.leaderstats.Deaths.Value then
DeathStore:SetAsync(player.UserId, player.leaderstats.Deaths.Value)
end
end
if player:FindFirstChild("leaderstats") then
if player.leaderstats:FindFirstChild("RC") and player.leaderstats.RC.Value then
RCStore:SetAsync(player.UserId, player.leaderstats.RC.Value)
end
end
end)
end)
game:BindToClose(function()
wait()
end)
Saving data in multiple datastores isn’t reliable due to rate limits, you may want to start using dictionaries to save player data in a single datastore:
local data = {
RC = 0,
Deaths = 0,
Team = "None"
}
local Players = game:GetService("Players")
local Teams = game:GetService("Teams")
local DataStores = game:GetService("DataStoreService")
local DataStore = DataStores:GetDataStore("DataStore")
local ProtectedCall = pcall
local function CreateStats(Player)
local Leaderstats = Instance.new("Folder")
Leaderstats.Name = "leaderstats"
Leaderstats.Parent = Player
local Team = Instance.new("StringValue")
Team.Name = "Team"
Team.Parent = Leaderstats
return true
end
local function SaveData(Player)
local Success, Result = ProtectedCall(function()
return DataStore:GetAsync("Team_"..Player.UserId, Player.Team.Name)
end)
if Success then
return
else
warn(Result)
end
end
local function LoadData(Player)
local Success, Result = ProtectedCall(function()
return DataStore:GetAsync("Team_"..Player.UserId)
end)
if Success then
if Result then
local Team = Teams:FindFirstChild(Result)
if Team then
Player.Team = Team
Player.leaderstats.Team.Value = Team.Name
end
end
else
warn(Result)
end
end
local function OnPlayerAdded(Player)
local _Stats = CreateStats()
repeat
task.wait()
until _Stats
LoadData(Player)
end
local function OnPlayerRemoving(Player)
SaveData(Player)
end
local function OnServerShutdown()
for _, Player in ipairs(Players:GetPlayers()) do
SaveData(Player)
end
end
Players.PlayerAdded:Connect(OnPlayerAdded)
Players.PlayerRemoving:Connect(OnPlayerRemoving)
game:BindToClose(OnServerShutdown)
Here’s a script I just created, it saves the player’s team whenever they leave the game and re-assigns them to that same team when they next rejoin the game, as an addition, the script also creates its own leaderstats which displays the names of teams each player belongs to.