This is how I would do it.
ModuleScript in ServerScriptService and I named it "TeamModule" -
03 | local DataStore 2 = require( 1936396537 ) |
05 | DataStore 2. Combine( "MainKey" , "Team" ) |
07 | local defaultTeam = "None" |
09 | function teamModule:SetPlayerTeam(player) |
10 | if player:IsA( "Player" ) then |
11 | local excessData = Instance.new( "Folder" ,player) |
12 | excessData.Name = "ExcessData" |
14 | local teamValue = Instance.new( "StringValue" ,excessData) |
15 | teamValue.Name = "Team" |
17 | local teamStore = DataStore 2 ( "Team" ,player) |
19 | local function updateTeam(team) |
20 | player.ExcessData.Team.Value = team |
23 | updateTeam(teamStore:Get(defaultTeam)) |
25 | teamStore:OnUpdate(updateTeam) |
27 | error ( "The First Parameter Has To Be A Player" ) |
This is a ServerScript in ServerScriptService and I named it TeamSet -
01 | local teamModule = require(script.Parent.TeamModule) |
03 | local DataStore 2 = require( 1936396537 ) |
05 | local RE = game:GetService( "ReplicatedStorage" ):WaitForChild( "Events" ) |
07 | game.Players.PlayerAdded:Connect( function (player) |
08 | teamModule:SetPlayerTeam(player) |
10 | local team = game:GetService( "Teams" ):FindFirstChild(player.ExcessData.Team.Value).TeamColor |
12 | player.TeamColor = team |
14 | player:LoadCharacter() |
17 | RE.TeamChange.OnServerEvent:Connect( function (player,team) |
18 | player.TeamColor = game:GetService( "Teams" ):FindFirstChild(team).TeamColor |
19 | player.ExcessData.Team.Value = team |
20 | local teamStore = DataStore 2 ( "Team" ,player) |
23 | player:LoadCharacter() |
And Then I have a folder in ReplicatedStorage called Events, and you should keep all RemoteEvents and Functions in there, and it will save their team. All you have to do is have the first team enabled Auto-Assignable, and the other teams should not have that enabled. It will get their team, and then respawn them on their team at that spawn. It should work, if it doesn't, let me know so I can tweak it and fix it for you. And for it to work in studio if you want to test it, you have to put a BoolValue inside of ServerStorage and name it SaveInStudio and have it enabled to true for it to work in studio.