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

What is wrong with this Team Captains script? It doesn't make anyone team captains?

Hey, so I'm making a game and at the beginning, once there are at least two players in the server, I want the server to pick two random players, both from the Free Agents team, put each one on a team, and make them the team captains for their respective teams. Here is my script so far... The first part works great but the part after "end" is the part that should be choosing the team captains and putting them on the teams "Home Team" and "Away Team" but it isn't working. It doesn't do anything, the players all stay on the "Free Agents" team and no one is made captain. Does anyone know what's wrong with this?

local replicatedstorage = game:GetService("ReplicatedStorage")
local status = replicatedstorage:WaitForChild("2orMoreNeeded")
local team1 = game.Teams["Home Team"]
local team2 = game.Teams["Away Team"]
local FreeAgents = game.Teams:WaitForChild("Free Agents"):GetPlayers()

while game.Players.NumPlayers < 2 do -- how many players needed for the game to start.
		status.Value = "Flag on the play! At least 2 players are required to begin!"
		repeat wait(2) until game.Players.NumPlayers >= 2 -- how many players are in the game.
		status.Value = "Picking Team Captains"
end
local HomeCap = FreeAgents[math.random(#FreeAgents)]
	HomeCap.Team = game.Teams["Home Team"]
local AwayCap = FreeAgents[math.random(#FreeAgents)]
	AwayCap.Team = game.Teams["Away Team"]

Thanks, Skyraider5

1 answer

Hello Skyraider5.

The reason that you script does not choose anyone to a team is because when a player is given a team it isn't being parented to that team. Instead you could do this:

local teamname = "FreeAgents"
local freeagents = {} -- All the players on the freeagents team (Not set yet)

freeagents = {}
for a, b in pairs(game.Players:GetChildren()) do -- Getting all the childs on the ravens team
	if b.Team == teamname then -- Check if the players is on the wanted team
		Table.Insert(freeagents,b) --Put him in the table
	end
end

local HomeCap = freeagents(math.random(1,#freeagents))
	HomeCap.Team = game.Teams["Home Team"]

freeagents = {}
for a, b in pairs(game.Players:GetChildren()) do -- Getting all the childs on the ravens team
	if b.Team == teamname then -- Check if the players is on the wanted team
		Table.Insert(freeagents,b) --Put him in the table
	end
end

local AwayCap = freeagents(math.random(1,#freeagents))
AwayCap.Team = game.Teams["Away Team"]

2 comments

0

table.insert isnt capitalized

0

it didn't work, the "Picking Team Captains" thing appears and then nothing happens, any idea why?

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