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
TimerEvent:FireAllClients(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 =
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 =
TimerEvent = ReplicatedStorage:WaitForChild("TimerEvent")
TimerEvent.OnClientEvent:Connect(function(Time)
timer.Text = Time
if Time == 0 then
timer.TextTransparency = 1
timer.BackgroundTransparency = 1
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"
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 = "Spectators"
SpectatorTeam.TeamColor = BrickColor.new("Medium stone grey")
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 PlayerTeam = math.random(1,2)
if PlayerTeam == 1 then
if RedTeamMemberCount >= MaxPlayersPerTeam then
player.Team = BlueTeam
BlueTeamMemberCount = BlueTeamMemberCount + 1
else
player.Team = RedTeam
RedTeamMemberCount = RedTeamMemberCount + 1
end
end
if PlayerTeam == 2 then
if BlueTeamMemberCount >= MaxPlayersPerTeam then
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)
wait(1)
Time = Time-1
if Time == 0 then
AssignTeams()
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
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)
else
player.Team = RedTeam
RedTeamMemberCount = RedTeamMemberCount + 1
character.HumanoidRootPart.Position = Vector3.new(-6.18, 1.5, -11.43)
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)
else
player.Team = BlueTeam
BlueTeamMemberCount = BlueTeamMemberCount + 1
character.HumanoidRootPart.Position = Vector3.new(-26.12, 1.5, -12.4)
end
end
end
end
TimerEvent = ReplicatedStorage:WaitForChild("TimerEvent")
Time = 30
for i = 1,31,1 do
TimerEvent:FireAllClients(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!
1 comment
Do you want equal players on each team?