This is how I would do it.
ModuleScript in ServerScriptService and I named it "TeamModule" -
local teamModule = {}
local DataStore2 = require(1936396537)
DataStore2.Combine("MainKey","Team")
local defaultTeam = "None"
function teamModule:SetPlayerTeam(player)
if player:IsA("Player") then
local excessData = Instance.new("Folder",player)
excessData.Name = "ExcessData"
local teamValue = Instance.new("StringValue",excessData)
teamValue.Name = "Team"
local teamStore = DataStore2("Team",player)
local function updateTeam(team)
player.ExcessData.Team.Value = team
end
updateTeam(teamStore:Get(defaultTeam))
teamStore:OnUpdate(updateTeam)
else
error("The First Parameter Has To Be A Player")
end
end
return teamModule
This is a ServerScript in ServerScriptService and I named it TeamSet -
local teamModule = require(script.Parent.TeamModule)
local DataStore2 = require(1936396537)
local RE = game:GetService("ReplicatedStorage"):WaitForChild("Events")
game.Players.PlayerAdded:Connect(function(player)
teamModule:SetPlayerTeam(player)
wait(1)
local team = game:GetService("Teams"):FindFirstChild(player.ExcessData.Team.Value).TeamColor
wait()
player.TeamColor = team
wait(0.25)
player:LoadCharacter()
end)
RE.TeamChange.OnServerEvent:Connect(function(player,team)
player.TeamColor = game:GetService("Teams"):FindFirstChild(team).TeamColor
player.ExcessData.Team.Value = team
local teamStore = DataStore2("Team",player)
teamStore:Set(team)
wait(0.1)
player:LoadCharacter()
end)
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.
1 comment
DataStore2 is easier because it is simplified down.