Hello! So I have a team assign script, but whenever I try to do an or statement, (2 teams the system can choose from), it errors. How can I use math.random() to randomize if a player is a certain rank, it will choose between 1 and another team? I can’t quite try anything right now, but if you could provide me with a script and show me what it does, it would be appreciated!
You could store a random number inside a variable, then use an if statement to check if that number is above or below or equal to another number, and then based on that result, assign the player to the appropriate team.
Example:
local teamNum = math.random(0, 255)
if teamNum <= 125 then
—assign to team 1
elseif teamNum > 125 then
—assign to team 2
end
Really sorry for the bad formatting I’m on my phone right now.
No problem! However, teams don’t have specific numbers. Also, checking if a team number is equal to something is not really what I’m looking for. I’ll tell you! So basically, I have 7 teams. If a player is a certain rank, I want it to randomize between 1 team and another. Would I be able to reference a team in with local Team1 = game.Teams["1"].BrickColor
, then use this: Player.TeamColor = math.random(Team1, :another team variable here:)
?
Math.random() only works for comparing and finding IntValues. A team is not an IntValue, so unfortunately I don’t think this would work. You could try and add an IntValue into each team and then compare those? I haven’t worked with teams much. Most of the time if I want to assign teams, I’ll just create my own system using StringValues and IntValues.
Do you know any way I can use an or
operator without it erroring? Heres my code:
Player.TeamColor = BrickColor.new("Mauve) or Player.TeamColor = BrickColor.new("Bright Orange")
Thats basically what I have, however, the second =
sign is underlined in red.
I’ve replied to a similar topic before. You can try using the function from my post here:
That is actually a really good topic, however, I can’t do random teams. Basically, here’s a rundown of what I need. A system checks if a player is a certain rank in my group. If they meet the rank, it will choose between 2 teams to give them, which will be randomized. My problem is that I can’t randomize between 2 teams.
I’m sure you know you can use the Player | Roblox Creator Documentation to check their rank. Then, assuming the two teams are only meant for players of a certain rank, you can define them somewhere in your code and hold them in a table.
local Teams = game:GetService("Teams")
local rankedTeams = {Teams:WaitForChild("RankedTeam1"), Teams:WaitForChild("RankedTeam2")}
--code assigning to random team.
player.Team = rankedTeams[math.random(#rankedTeams)]
That makes SO much sense! I will let you know any issues when I can try this out! Thank you so much!
One more thing, the team names are numbers only, but when I add them into a script, it looks something like this: game.Teams:FindFirstChild(%24")
, when I input it as game.Teams:FindFirstChild("1")
. I don’t really know what is up with that.
No problem. Also, if you want to have the amount of players in each team balanced then you can use a modified version of the function I gave you.
local Players = game:GetService("Players")
local Teams = game:GetService("Teams")
local function RandomizeTeams(players, teams)
players = if players~=nil then players else Players:GetPlayers()
local teams = if teams~=nil then teams else Teams:GetTeams()
local function assignLeftoverTeams()
for _,player in pairs(players) do
local teamIndex = math.random(#teams) -- get random team
local team = teams[teamIndex]
player.Team = team
table.remove(teams, teamIndex)
end
end
if #teams >= #players then
assignLeftoverTeams()
else
local splitAmount = (#players-(#players % #teams)) / #teams -- the amount to split the players by
for team in pairs(teams) do -- assign the players evenly to each team
for i=1,splitAmount do
local playerIndex = math.random(#players)
local player = players[playerIndex]
player.Team = team
table.remove(players, playerIndex)
end
end
assignLeftoverTeams()
end
end
RandomizeTeams({player}, rankedTeams)
Do note that I haven’t actually tested the edited function and haven’t fully thought through the changes I made to it, so it may not even work.
If the team names are numbers then you can sort them in a much more efficient way. Instead of using :FindFirstChild
for each team you can use Teams:GetTeams
. Then, you can sort the table using table.sort
. So something like this:
local teams = Teams:GetTeams()
table.sort(teams, function(team1, team2) return tonumber(team1.Name) < tonumber(team2.Name) end)
Then when you want to access a team, you can just access the array by the team number. Like so:
teams[24]
By default, math.random()
returns a number between 0
and 1
, so this could be optimized by accounting for that (and rounded to remove the bias towards “team 1” in your implementation).
Team instances share a “AutoAssignable” property which specifies whether or not players should be randomly assigned to the team.
https://developer.roblox.com/en-us/api-reference/property/Team/AutoAssignable
local random = math.random(1, 2)
if random == 1 then
Player.TeamColor = BrickColor.new("Mauve")
elseif random == 2 then
Player.TeamColor = BrickColor.new("Bright Orange")
end
That error is fairly easy to circumvent by generating a random number to represent randomness.