Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do you change someones team randomly?

Asked by
TheePBHST 154
7 years ago
Edited by OldPalHappy 7 years ago

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.

0
:D OldPalHappy 1477 — 7y

1 answer

Log in to vote
1
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
7 years ago
Edited 7 years ago

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
0
Question, how do I get the script to gather all players and team them randomly and make only one person in each team? TheePBHST 154 — 7y
0
You would need to get tables of all players on both teams, and then manipulate the code above to index those tables. Goulstem 8144 — 7y
Ad

Answer this question