I need help with my code

I am making a swords vs guns game and it wont change the teams. Here is the script

--Variables--

local Teams = game:GetService("Teams"):GetChildren()
local TextBox = script.Parent

--Variables--

--Funtions--

function GenorateTeam()
	for i, player in pairs(game.Players:GetPlayers() ) do
		if player then
			if player.Team == nil then
				local randomteam = Teams[math.random(1, #Teams)]
				player.Team = randomteam
				GameText2()
			end
		end
	end
end

function GameText()
	for i = 30, 0, -1 do
		print(i.. "one")
		TextBox.Text = "Game Starting in ".. i		
		wait(1)
		print("waited")
	end
	TextBox.Text = "Randomizing teams"
	GenorateTeam()
end

function GameText2()
	wait(2)
	TextBox.Text = "Plugging group"
	wait(0.5)
	TextBox.Text = "JOIN NOW"
	wait(0.01)
	TextBox.Text = "Giving  gears"
	wait(2)
	game.Teams.ClassicSword.Parent = game.Teams.Melee
	game.Teams.Handgun.Parent = game.Teams.Gunners
end

GameText()

The text label just stops on “Randomizing Teams…”

Try this:

local randomteam = Teams[math.random(1, #Teams)]
				player.Team = Teams[randomteam]

The Reason is not working is because you are trying to set a Player’s Team to a number and not a Team. #Teams just simply gets the number of all the Items in the Table.

The code doesn’t stop and goes on, but the player is not on the team.

just do this:

local TeamsTable = {}
table.insert(TeamsTable, Teams) -- Inserts all Teams into a Table for use

function GenorateTeam()
for i, player in pairs(game.Players:GetPlayers() ) do
		if Player.Team == nil then
				local randomteam = TeamsTable[math.random(1, #TeamsTable)]
				player.Team = TeamsTable[randomteam]
				GameText2()
			end
	end
end

this part says that “Player” Is an unknow globle

sorry, its player, not Player, Capitalization issue.

1 Like
function GenorateTeam()
	for i, player in pairs(game.Players:GetPlayers() ) do
		if player then
			if player.Team == Teams.Neutral then
				player.Team = Teams:GetTeams()[math.random(1, #Teams:GetTeams()]
				GameText2()
			end
		end
	end
end

It wont change the team still. :sad:

There is an error with the line

player.Team = Teams:GetTeams()[math.random(1, #Teams:GetTeams()]

At the top of your script, change Teams to game:GetService("Teams")

Close “math.random”

New line:

player.Team = Teams:GetTeams()[math.random(1, #Teams:GetTeams())]