A button that randomize players in a server into different Teams

So I making a training facility and im trying to make a button that randomize players into different teams. Can some on help me?

Server Script:

local rs = game.ReplicatedStorage.TeamEvent
local team1 = game.Teams.Team1
local team2 = game.Teams.Team2

rs.OnServerEvent:Connect(function()
	game.Players.PlayerAdded:Connect(function(player)
		local randomTeam = team1 and team2
		
		player.Team = randomTeam
	end)
end)

Local Script:

local button = script.Parent
local rs = game.ReplicatedStorage.TeamEvent

button.MouseButton1Click:Connect(function()
	rs:FireServer()
end)

Any Help is appreciated!

To get a random team from the 2 choices, you can do a quick short-circuit evaluation with math.random(), like so:

local PickedTeam = math.random(1,2) == 1 and team1 or team2

ok let me test it. thanks for the reply

it didnt work i have turned off auto assignable on the two teams

Lobby - Auto-Assignable
Team1 - Off
Team2 - Off

between team1 and team2 is where i will randomize the players in the server

local teams = game.Teams:GetChildren() --GetChildren returns an array

--getting a random index using the built-in pseudorandom function
local index = math.random(1, #teams)
local randomTeam = teams[index] --indexing the array/table with that specific index

player.Team = randomTeam

you know what squash it. It will never work

Are you trying to randomly generate players evenly across the different teams or just randomly generate them onto different teams? If you’re trying to generate them randomly without it being dispersed evenly you can generate a number between 0 and 1 and if the number is greater than 0.5 they will be put onto a Team 2 and if its lower they get put on Team 1.

yes that was before but i got something else in mind