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

Local Team Changer Not Working?

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

1 comment

0

Line 2-3 are just string formats.

2 answers

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

4 comments

0

It seems not to be running inside my local script? No errors, its all correct, just not running properly? any ideas?

0

Hmm...make sure that workspace.Gamestart is set to true. That's the only problem I can see. And also make sure there's an "end" at the end of this code block.

0

I still dont seem to have it working both teams have auto assigning off so every starts as nuatral, i updated it to the most recent code block. Any Ideas now?

0

Can you message me on Roblox so I can get some more information about what's going on?

##Problem

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..

  1. No argument: Calling it with no supplied argument would returned a uniformly distributed pseudo-random numbers.

  2. One argument "maximum": Calling it with just one argument would return a uniform pseudo-random integer in ranging from 1 to maximum.

  3. 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 (#).

##Solution

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")

2 comments

0

Thanks its working but its not changing on the leaderboard, but the team's tools are being given

0

What? Their TeamColor is changed but not on the leaderboard? Are you using roblox's default board?

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