Hello developers! I am basically trying to make it so only 6 people can be on one team. I have Team1 and Team2. 12 people can be in the server, and 6 on each team. How would I do this? I turned off auto assign and got to scripting, but my methods dont work. Sadly I deleted the script so I cant show what I had
local PlayersInTeam = #Team:GetPlayers() -- Returns an int value, equal or bigger than 0.
If there are too many players in the team do not team them to that team, OR if there are too many players then move one out.
To set a player’s team use
Player.Team = Team -- Team being a variable refering to said team.
Thats how to get the amount of players in a team. Thank you. Now I just have to figure out how to limit it. Its probably gonna look like this:
local PlayersInTeam = #Team:GetPlayers()
If PlayersInTeam = 6 then
--limit the players somehow
Correct?
Code you could use to set a player’s team on join:
local TeamService = game:GetService("Teams")
game:GetService("Players").PlayerAdded:Connect(function(player)
local PossibleTeams = {} -- [#Plrs] = Team
for _, Team in pairs(TeamService:GetTeams())
local PlayersInTeam = #Team:GetPlayers() -- How many players there are in said team
if PossibleTeams[PlayersInTeam] and math.random(2) == 2 then
-- if already value then 50/50
PossibleTeams[PlayersInTeam] = Team
elseif not PossibleTeams[PlayersInTeam] then
-- if no value then set the value
PossibleTeams[PlayersInTeam] = Team
end
end
for _, possibleTeam in ipairs(PossibleTeams) -- ipairs so it starts from the lowest to the highest
player.Team = possibleTeam
break -- Stop the for loop, because he now already has a team.
end
end)
if PlayersInTeam < 6
-- can access
end
The above is correct, note every time you check the value you have to update it/set it/define it again.
Im sorry, I am quite new to Luau, what does this line of code even do?
if PossibleTeams[PlayersInTeam] and math.random(2) == 2 then
I am using tables.
math.random(2) -- either 1 or 2 (is an int, aka nothing in between)
so that means it’s 50/50 that it changes.
if PossibleTeams[PlayersInTeam] then
-- is the same as
if PossibleTeams[PlayersInTeam] ~= nil then
-- so if it exists then.
I am using tables in this case, you can add as many teams as you want to it.
The table would look like
local PossibleTeams = {
[4] = Team1, -- Team1 has 4 players
[5] = Team2, -- Team2 has 5 players.
}
PossibleTeams[4] -- returns Team1
This does work. Thank you. I might modify slightly if needed.
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.