I'm creating a script whereas only one player can be in a specific team via a team switching GUI (presumably a click button one). I want to make the limit "one", and if there's already one player occupying that spot, no one else can join that team, no matter how hard they click the team switching button for it. In case you're wondering, here's some details:
player = game.Players.LocalPlayer function Click(mouse) script.Parent.Sound:Play() if player.TeamColor == BrickColor.new("Reddish brown") then return nil else player.TeamColor = BrickColor.new("Reddish brown") wait() player.Character.Humanoid.Health = 0 end end function MouseLeave() script.Parent.ImageLabel.ImageTransparency = 0 script.Parent.Words.Visible = false end function MouseEnter() script.Parent.ImageLabel.ImageTransparency = 0.5 script.Parent.Words.Visible = true end script.Parent.MouseButton1Click:connect(Click) script.Parent.ImageLabel.MouseLeave:connect(MouseLeave) script.Parent.ImageLabel.MouseEnter:connect(MouseEnter)
I want to add in an command that will prevent a player from switching to that team if another player occupies that spot. I'll be so grateful. Thanks.
You can check if the length of Team:GetPlayers()
is equal to or exceeds the max number of players you want in one team.
Also, if your game is fe, you should handle the team change via a server script, like so:
Server:
local teamEvent = Instance.new("RemoteEvent", game:GetService("ReplicatedStorage")) teamEvent.Name = "teamEvent" local limits = { ["Border Inspector"] = 1, } teamEvent.OnServerEvent:Connect(function(plr, teamName) local team = game:GetService("Teams"):FindFirstChild(teamName) if not team then return end if limits[teamName] and #team:GetPlayers() >= limits[teamName] then return --abort if the team name is in the limits table and the number of players in that team is greater than or equal to the number in the table end plr.Team = team --otherwise set their team end)
Client:
local teamRemote = game:GetService("ReplicatedStorage"):WaitForChild("teamRemote") local function ChangeTeam(name) teamRemote:FireServer(name) end --somewhere else in your code: ChangeTeam("Border Inspector")