TeamService not working

even tough i make it so it needs there to be 0 people in the team to be able to join it still allows me to join

local Settings = require(script.Parent.Parent.Settings)

local AllPlayerCards = game.Players.LocalPlayer.PlayerGui.ClassSelection.GUIBase:WaitForChild("MiddleBig")
local Display = game.Players.LocalPlayer.PlayerGui.ClassSelection.GUIBase:WaitForChild("MiddleSmall")
local Teams = game:GetService("Teams")

local P1Team = Teams["Player1"]:GetPlayers()
local PlayersOnTeam1 = #P1Team
local function P1TR()
	print(PlayersOnTeam1)
end

local P2Team = Teams["Player2"]:GetPlayers()
local PlayersOnTeam2 = #P2Team
local function P2TR()
	print(PlayersOnTeam2)
end

local P3Team = Teams["Player3"]:GetPlayers()
local PlayersOnTeam3 = #P3Team
local function P3TR()
	print(PlayersOnTeam3)
end

local P4Team = Teams["Player4"]:GetPlayers()
local PlayersOnTeam4 = #P4Team
local function P4TR()
	print(PlayersOnTeam4)
end

local P5Team = Teams["Player5"]:GetPlayers()
local PlayersOnTeam5 = #P5Team
local function P5TR()
	print(PlayersOnTeam5)
end

local P6Team = Teams["Player1"]:GetPlayers()
local PlayersOnTeam6 = #P6Team
local function P6TR()
	print(PlayersOnTeam6)
end

AllPlayerCards.Player1Select.TextButton.MouseButton1Click:Connect(function()
	P1TR()
	if PlayersOnTeam1 == 0  then
		Display.CName.Text = Settings.Player1Name
		Display.CImage.Image = Settings.Player1Image
		Display.CImage.ImageColor3 = Color3.fromHex("#ffffff")
		script.Parent.TeamChanger.Team.Value = 1
	else
		AllPlayerCards.Player1Select.TextButton.Text = "Character already in use!"
		wait(3)
		AllPlayerCards.Player1Select.TextButton.Text = Settings.Player1Name
	end
end)

AllPlayerCards.Player2Select.TextButton.MouseButton1Click:Connect(function()
	P2TR()
	if PlayersOnTeam2 == 0  then
		Display.CName.Text = Settings.Player2Name
		Display.CImage.Image = Settings.Player2Image
		Display.CImage.ImageColor3 = Color3.fromHex("#ffffff")
		script.Parent.TeamChanger.Team.Value = 2
	else
		AllPlayerCards.Player2Select.TextButton.Text = "Character already in use!"
		wait(3)
		AllPlayerCards.Player2Select.TextButton.Text = Settings.Player2Name
	end
	
end)

AllPlayerCards.Player3Select.TextButton.MouseButton1Click:Connect(function()
	P3TR()
	if PlayersOnTeam3 == 0  then
		Display.CName.Text = Settings.Player3Name
		Display.CImage.Image = Settings.Player3Image
		Display.CImage.ImageColor3 = Color3.fromHex("#ffffff")
		script.Parent.TeamChanger.Team.Value = 3
	else
		AllPlayerCards.Player3Select.TextButton.Text = "Character already in use!"
		wait(3)
		AllPlayerCards.Player3Select.TextButton.Text = Settings.Player3Name
	end
end)

AllPlayerCards.Player4Select.TextButton.MouseButton1Click:Connect(function()
	P4TR()
	if PlayersOnTeam4 == 0  then
		Display.CName.Text = Settings.Player4Name
		Display.CImage.Image = Settings.Player4Image
		Display.CImage.ImageColor3 = Color3.fromHex("#ffffff")
		script.Parent.TeamChanger.Team.Value = 4
	else
		AllPlayerCards.Player4Select.TextButton.Text = "Character already in use!"
		wait(3)
		AllPlayerCards.Player4Select.TextButton.Text = Settings.Player4Name
	end
end)

AllPlayerCards.Player5Select.TextButton.MouseButton1Click:Connect(function()
	P5TR()
	if PlayersOnTeam5 == 0  then
		Display.CName.Text = Settings.Player5Name
		Display.CImage.Image = Settings.Player5Image
		Display.CImage.ImageColor3 = Color3.fromHex("#ffffff")
		script.Parent.TeamChanger.Team.Value = 5
	else
		AllPlayerCards.Player5Select.TextButton.Text = "Character already in use!"
		wait(3)
		AllPlayerCards.Player5Select.TextButton.Text = Settings.Player5Name
	end
end)

AllPlayerCards["Player6Select(Monster)"].TextButton.MouseButton1Click:Connect(function()
	P6TR()
	if #P6Team < 1 then
		Display.CName.Text = Settings.Player6Name
		Display.CImage.Image = Settings.Player6Image
		Display.CImage.ImageColor3 = Color3.fromHex("#ff0000")
		script.Parent.TeamChanger.Team.Value = 6
		print("Accepted")
	else
		AllPlayerCards["Player6Select(Monster)"].TextButton.Text = "Character already in use!"
		wait(3)
		AllPlayerCards["Player6Select(Monster)"].TextButton.Text = Settings.Player6Name
		print("Rejected")
	end
end)

Code Was Altered

2 Likes

you have to check the number of players again each time the event is fired

local team = Teams["exampleTeam"]

exampleEvent:Connect(function()
  local teamPlayers = team:GetPlayers()
  local numPlayers = #teamPlayers

  print(numPlayers) -- this updates every time the event is fired now
end)
1 Like

friend is offline rn cant test it can you look at the code i edited it so its the newer one it used a normal function instead of a event is that fine?

1 Like

this part where you get the players in the team needs to be inside of the event

yeah it doesnt work for some reason