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

I'd like to ask for help about certain scripting techniques?

So to this question, I'd like to ask if you could help me out by understanding this:

How would I find when the team's eliminated?

Ex:

if T2.NumPlayers == nil then

T2 represents "Team 2", if you're curious.

Second question, how do I change players to team blue, and red evenly, once the game starts? I don't have an example for this, I haven't figured it out.

Third question, Is there any mistakes in this script?

print( game.Players.NumPlayers )

 

local x = game.Players

print(

    "There "

    .. (x.NumPlayers == 1 and "is" or "are") .. " " .. x.NumPlayers

    .. " player" .. (x.NumPlayers == 1 and "" or "s") .. " playing"
      )

function Even()
	SelectPlayers = game.Players:GetChildren()
	Number = 0
	for i=1, #SelectPlayers do
	Number = Number + 1
	if Number > #Teams then Number = 1 end
	SelectPlayers[i].TeamColor = Teams[Number].TeamColor
end
end

A = game.Players

if A.NumPlayers == 1 then print("There's not enough players in the game for the game to start.")
A2 = "Welcome to the official meadows sfing game"
A4 = "5"
A3 = "The amount of players is successful, and the game will be starting in" .. A4 .. "Seconds.."


elseif not NumPlayers < 1 then
	print(NumPlayers.." Is the amount of players in the game.")
	wait(0.1)
	if A.NumPlayers == NumPlayers >2 then
	print("Success")
	wait(3)
	elseif A.NumPlayers == NumPlayers <1 then
    print("Denial")
    wait(3)
	while true do
	local M = Instance.new("Message",workspace)
	M.Text = A2
	wait(1)
	M.Text = A3
	wait(0.1)
	local T1 = Instance.new("Team",Teams)
	T1.TeamColor = "Bright blue"
	wait(0.1)
	local T2 = Instance.new("Team",Teams)
	T2.TeamColor = "Bright red"
	-- Work in progress...
	wait(3)
Even()
end
end
end

1 comment

0

Is there anything wrong with the bottom script? If there is please do fix it.

1 answer

To evenly distribute each player on each team you need to divide the number of players by the number of teams, however, this method won't always work because you might get a decimal number, which you can compensate for by rounding down, and by randomly choosing a team for the left over players. Which would look something like this:

-- Evenly distribute players on each team.
players = game.Players:GetChildren()
Teams = {BrickColor.new("Bright red"), BrickColor.new("Bright blue")}
loopAmt = math.floor(#players / #Teams) * #Teams
remaining = #players - loopAmt
currTeam = Teams[1]
for i = 1, loopAmt do
	if(i == (loopAmt / #Teams)) then
		currTeam = Teams[2]
	end
	players[i].TeamColor = currTeam
end
for i = loopAmt + 1, loopAmt + remaining do
	players[i].TeamColor = Teams[math.random(1, 2)]
end

Now, if we want to check if a whole team is eliminated efficiently, we would use a Changed event, to only do our code when their TeamColor changes.

function checkTeamSizes()
	BlueTeamSize = 0
	RedTeamSize = 0
	for _, player in pairs(game.Players:GetChildren()) do
		if(player.TeamColor == BrickColor.new("Bright blue")) then
			BlueTeamSize = BlueTeamSize + 1
		else
			RedTeamSize = RedTeamSize + 1
		end
	end
	if(BlueTeamSize == 0) then
		-- Remove blue team
	elseif(RedTeamSize == 0) then
		-- Remove red team
	end
end

game.Players.PlayerAdded:connect(function(player)
	player.DescendantAdded:connect(function()
		player.TeamColor.Changed:connect(function(color)
			checkTeamSizes()
		end)
	end)
end)

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