Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do you change someones team randomly?

So, I was making a game and in my mind, I wondered how you can change players team randomly. I know how to change it but not the scripts choice.

Is there one that have a approved answer?

I thought of something like this - "Example: game.Players.Player1.TeamColor" Mind putting everything like LocalPlayers and etc.

Thank you.

1 comment

0

:D

1 answer

✓ Accepted answer

Yes, you can do this using the math.random function. This function takes in two arguments and returns a randomized number between them.

Setup a randomized wait time, as well as the random character to change teams, and randomly manipulate the team they reside in.

local minWait = 10
local maxWait = 50

while wait(math.random(minWait,maxWait)) do
	local currentPlayers = game.Players:GetPlayers() --Collect all players
	local currentTeams = game.Teams:GetChildren() --Collect all teams
	--Get a random player, and a random team
	local randomPlayer = currentPlayers[math.random(1,#currentPlayers)]
	local randomTeam = currentTeams[math.random(1,#currentTeams)]
	--Manipulate the random player's team to the random team.
	randomPlayer.Team = randomTeam
end

Note: In order to ensure the randomized team will not be the same as the randomized player's current team, you will have to add a while loop.

while randomPlayer.Team == randomTeam and wait() do
	randomTeam = currentTeams[math.random(1,#currentTeams)]
end

randomPlayer.Team = randomTeam

2 comments

0

Question, how do I get the script to gather all players and team them randomly and make only one person in each team?

0

You would need to get tables of all players on both teams, and then manipulate the code above to index those tables.

This discussion is preserved as it was. New answers, comments, and votes are closed.