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

How do I make a team creating countdown timer?

The main question was very vague, so I'll explain it here:

I basically want to make a timer that counts down from a specific number. When this timer reaches 0, I want the players to be spawned at different team spawn locations. I have the timer working so far, but I still do not know how to tie that to creating random teams. Here is the script I have so far:

local timer = script.Parent.Parent.Timer
local seconds = script.Parent.Parent.Time
script.Parent.Text = seconds.Value
local SpectatorSpawn = workspace.SpectatorSpawn
local RedSpawn = workspace.RedSpawn
local BlueSpawn = workspace.BlueSpawn

timer.BackgroundTransparency = .3
seconds.Value = 30
repeat
	for i = 1,seconds.Value do
		wait(1)
		seconds.Value = seconds.value - 1
		script.Parent.Text = seconds.Value
	end
	if seconds.Value == 0 then	
		wait(1)
		seconds.Value = 30
	end

until timer.Visible == false

1 comment

0

Do you want equal players on each team?

1 answer

In order to do that, the timer would have to be in a server script, or else individual timers would be running for each player, assigning teams at different times for each player. So what you would need to do is have the timer on a server script, and use a remote event to change the Gui on the local script. So first of all, add a "RemoteEvent" into "ReplicatedStorage", and name it "TimerEvent", or something else if you wish. Then, create a server script and make a timer script in it. This is what I would recommend doing:

SERVER SCRIPT

ReplicatedStorage = game:GetService("ReplicatedStorage")

TimerEvent = ReplicatedStorage:WaitForChild("TimerEvent")

Time = 30 

for i = 1,31,1 do -- I put 31 so zero would be a value
	TimerEvent:FireAllClients(Time) -- Fires Remote Event to all the Clients, sending "Time"
	wait(1)
	Time = Time-1
end

The reason for having the timer in the server is so that it is synced for everyone in your game. Now in a local script, you need to code the Gui:

LOCAL SCRIPT

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

local player = Players.LocalPlayer

local PlayerGui = player:WaitForChild("PlayerGui")
local timer = -- Text Label Gui that Shows the Time left in the Timer

TimerEvent = ReplicatedStorage:WaitForChild("TimerEvent")

TimerEvent.OnClientEvent:Connect(function(Time)
	timer.Text = Time
end)

If you want the timer Gui to disappear after it reaches zero, then make variables for all the Gui's you want to disappear, then change your local script to this:

LOCAL SCRIPT

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

local player = Players.LocalPlayer

local PlayerGui = player:WaitForChild("PlayerGui")
local timer =  -- Text Label Gui that Shows the Time left in the Timer

TimerEvent = ReplicatedStorage:WaitForChild("TimerEvent")

TimerEvent.OnClientEvent:Connect(function(Time)
	timer.Text = Time
	if Time == 0 then
		timer.TextTransparency = 1
		timer.BackgroundTransparency = 1
		-- Anything else you need to make transparent
	end
end)

Now to code the teams. The teams will run from the server script, when the timer is done. This is the updated version of the server script that will also assign teams:

SERVER SCRIPT

Teams = game:GetService("Teams")
ReplicatedStorage = game:GetService("ReplicatedStorage")
Players = game:GetService("Players")

BlueTeam = Instance.new("Team",Teams)
BlueTeam.Name = "Blue"           -- Creates Blue Team
BlueTeam.TeamColor = BrickColor.new("Really blue")  -- Team Color

RedTeam = Instance.new("Team",Teams)
RedTeam.Name = "Red"           -- Creates RedTeam
RedTeam.TeamColor = BrickColor.new("Really red")   -- Team Color

SpectatorTeam = Instance.new("Team",Teams)
SpectatorTeam.Name = "Spectators"    -- Crates Spectator Team
SpectatorTeam.TeamColor = BrickColor.new("Medium stone grey") -- Team Color

Players.PlayerAdded:Connect(function(plr)
	plr.Team = SpectatorTeam -- Automatically puts new players in the spectators team
end)

function AssignTeams()  -- This Function assigns teams to the players
	
	local PlayerCount = 0
	
	for i, player in pairs (Players:GetPlayers()) do
		PlayerCount = PlayerCount + 1  -- Gets Player Count
	end
	
	local MaxPlayersPerTeam = PlayerCount/2  -- Max players per team
	
	local BlueTeamMemberCount = 0 
         -- These are made to keep track of the players in a team
	local RedTeamMemberCount = 0
	
	for i, player in pairs (Players:GetPlayers()) do  -- runs once for every player in the game, "player" is the player who is going through the loop at that time
		local PlayerTeam = math.random(1,2) -- Makes the team random, 1 = Red, 2 = Blue
		if PlayerTeam == 1 then
			if RedTeamMemberCount >= MaxPlayersPerTeam then -- If there are already too many players on red team, the player will be assigned blue
				player.Team = BlueTeam
				BlueTeamMemberCount = BlueTeamMemberCount + 1
			else
				player.Team = RedTeam
				RedTeamMemberCount = RedTeamMemberCount + 1
			end
		end
		if PlayerTeam == 2 then
			if BlueTeamMemberCount >= MaxPlayersPerTeam then  -- Same thing as with Red team
				player.Team = RedTeam
				RedTeamMemberCount = RedTeamMemberCount + 1
			else
				player.Team = BlueTeam
				BlueTeamMemberCount = BlueTeamMemberCount + 1
			end
		end
	end
end

TimerEvent = ReplicatedStorage:WaitForChild("TimerEvent")

Time = 30

for i = 1,31,1 do
	TimerEvent:FireAllClients(Time) -- Fires Remote Event to all the Clients, sending "Time"
	wait(1)
	Time = Time-1
	if Time == 0 then
		AssignTeams()  -- Runs the Function Above
	end
end

Assuming you want players teleported to their spawns at the end of the timer, now all that needs to be done is to teleport the players. I don't know if you've done this already, but add 3 spawns into workspace. Name the first "SpectatorSpawn", the second "RedSpawn", and the last "BlueSpawn". Put them wherever you wish. Then go into properties for "Spectator Spawn", and find the "TeamColor" property, which will assign the spawn to a certain team. Make the team color for the spectator spawn "Medium stone grey", as it is the Team Color of the spectators team. Then do the same for the Blue Spawn and the Red Spawn, but the color for Blue Spawn should be "Really blue", and the Red Spawn should be "Really red", as was assigned above when the teams were created in the script. If you wish to change the team colors, change it in the server script as well as on the spawns, or else it won't work properly. This process will make each spawn a team spawn, so only people of that team will spawn there. This is the last addition to the server script to teleport the players:

SERVER SCRIPT

Teams = game:GetService("Teams")
ReplicatedStorage = game:GetService("ReplicatedStorage")
Players = game:GetService("Players")

BlueTeam = Instance.new("Team",Teams)
BlueTeam.Name = "Blue"
BlueTeam.TeamColor = BrickColor.new("Really blue")

RedTeam = Instance.new("Team",Teams)
RedTeam.Name = "Red"
RedTeam.TeamColor = BrickColor.new("Really red")

SpectatorTeam = Instance.new("Team",Teams)
SpectatorTeam.Name = "Neutral"
SpectatorTeam.TeamColor = BrickColor.new("Medium stone grey")

RedSpawn = workspace:WaitForChild("RedSpawn")
BlueSpawn = workspace:WaitForChild("BlueSpawn")

Players.PlayerAdded:Connect(function(plr)
	plr.Team = SpectatorTeam
end)

function AssignTeams()
	
	local PlayerCount = 0
	
	for i, player in pairs (Players:GetPlayers()) do
		PlayerCount = PlayerCount + 1
	end
	
	local MaxPlayersPerTeam = PlayerCount/2
	
	local BlueTeamMemberCount = 0
	local RedTeamMemberCount = 0
	
	for i, player in pairs (Players:GetPlayers()) do
		local character = player.Character -- Physical character of the player
		local PlayerTeam = math.random(1,2)
		if PlayerTeam == 1 then
			if RedTeamMemberCount >= MaxPlayersPerTeam then
				player.Team = BlueTeam
				BlueTeamMemberCount = BlueTeamMemberCount + 1
				character.HumanoidRootPart.Position = Vector3.new(-26.12, 1.5, -12.4)   -- Insert the position of your Blue Spawn inside these parenthesis, but add 1 to the second value (the y value) so the character gets teleported on top of it, not inside of it. So the spawn position I made up has 0.5 as the second value, but I made it 1.5
			else
				player.Team = RedTeam
				RedTeamMemberCount = RedTeamMemberCount + 1
				character.HumanoidRootPart.Position = Vector3.new(-6.18, 1.5, -11.43)    -- Same thing as with Blue, copy and paste the Red Spawn position inside the parenthesis, and add 1 to the second value
			end
		end
		if PlayerTeam == 2 then
			if BlueTeamMemberCount >= MaxPlayersPerTeam then
				player.Team = RedTeam
				RedTeamMemberCount = RedTeamMemberCount + 1
				character.HumanoidRootPart.Position = Vector3.new(-6.18, 1.5, -11.43)    -- The Red Spawn position, same as you have it above
			else
				player.Team = BlueTeam
				BlueTeamMemberCount = BlueTeamMemberCount + 1
				character.HumanoidRootPart.Position = Vector3.new(-26.12, 1.5, -12.4)   -- Blue Spawn position again, don't forget to add 1!
			end
		end
	end
end

TimerEvent = ReplicatedStorage:WaitForChild("TimerEvent")

Time = 30

for i = 1,31,1 do
	TimerEvent:FireAllClients(Time) -- Fires Remote Event to all the Clients, sending "Time"
	wait(1)
	Time = Time-1
	if Time == 0 then
		AssignTeams()
	end
end

And that's the full scripts, I hope that's what you wanted to happen. If it's not, player message me on roblox, or look at tutorials to learn how to make it yourself. Good luck with your game!

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