This script is a (horribly Failed) attempt at a team changer as a addition onto my local script. Thanks for reading. Code here(I know its bad)
if workspace.Gamestart == true then Rteam = BrickColor.new("Bright red") Bteam = BrickColor.new("Bright blue") Teams = {Rteam,Bteam} local plr = game:GetService("Players").LocalPlayer plr.TeamColor = Teams[math.random(#Teams)] end
You used the wrong type of Color Format for the teams. You need to use proper BrickColor formatting. Try this instead:
Rteam = BrickColor.new("Bright red") Bteam = BrickColor.new("Bright blue")
Also, you used math.random incorrectly to get the team. To fix this, let's put this in:
plr.TeamColor = Teams[math.random(#Teams)]
Anyways, I believe it should work now. If you have any further questions/problems, please leave a comment below! Hope I helped :P
You are simply attempting to set the player's TeamColor property to whatever string is returned by the randomly generated value from table Teams
. In order for your assignment to work, your random TeamColor must be a BrickColor data type, which you can set using its .new
constructor function.
The random function of the math library works one of three ways..
No argument: Calling it with no supplied argument would returned a uniformly distributed pseudo-random numbers.
One argument "maximum": Calling it with just one argument would return a uniform pseudo-random integer in ranging from 1 to maximum.
Two arguments "minimum, maximum": Calling it with two arguments would return a uniform pseudo-random integer ranging from minimum to maximum
All arguments must be integers
. Your code isn't functioning as expected because you are passing a table, team
, as argument to the function instead of the length
of said table, with can be determined using the length operator (#).
local redTeam, blueTeam = BrickColor.new("Bright red"), BrickColor.new("Bright blue") local player = game:GetService("Players").LocalPlayer local teams = {redTeam, blueTeam} player.TeamColor = teams[math.random(1, #teams)]
You could also assign a variable to the TeamColor for use later on in your script.. by storing the pseudo-random integer before indexing it.
local redTeam, blueTeam = BrickColor.new("Bright red"), BrickColor.new("Bright blue") local player = game:GetService("Players").LocalPlayer local teams = {redTeam, blueTeam} local color = math.random(1, #teams) player.TeamColor = teams[color] print(player.Name, "has been assigned to the", tostring(color),"team")