Starting game when there is a player count of 2

Hi I am trying to make the code in my game work it’s kind of like a minigame system but I want it to run when there are 2 players in-game.

wait(1.5)

local Players = game:GetService("Players")
local plrs = game.Players:GetPlayers()
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local StarterGui = game:GetService("StarterGui")
local TweenService = game:GetService("TweenService")


local IntermissionEvent = script.Parent.Intermission
local StartGameEvent = script.Parent.StartGame

local ValuesFolder = ReplicatedStorage:WaitForChild("Values")
local Status = ValuesFolder:WaitForChild("Status")
local InIntermission = ValuesFolder:WaitForChild("InIntermission")
local inGame = ValuesFolder:WaitForChild("InGame")
local TeleportTeamPlayers = script.Parent.TeleportTeamPlayers
local NotEnoughPlayersEvent = script.Parent.NotEnoughPlayers

local HomeScore = ValuesFolder:WaitForChild("HomeTeamScore")
local AwayScore = ValuesFolder:WaitForChild("AwayTeamScore")

local HomeSpawn = game.Workspace:WaitForChild("Home")
local AwaySpawn = game.Workspace:WaitForChild("Away")

local Teams = game:GetService("Teams")

local HomeGoalTrigger = game.Workspace:WaitForChild("HomeGoalTrigger")
local AwayGoalTrigger = game.Workspace:WaitForChild("AwayGoalTrigger")
local Ball = game.Workspace.TPS

local TPS = Ball:Clone()




local function GiveTeamWins(TeamName,Amount)
	for _,player in pairs(game.Players:GetChildren()) do
		if player.Team.Name == TeamName then
			print(player.Team.Name .. " Has won the game")
			player.stats.Wins.Value += Amount
		end
	end
end

local function StartingGame()
	Ball.Position = Vector3.new(221.21, 1.944, -437.77)
	Ball.AssemblyAngularVelocity = Vector3.new(0, 0, 0)
	Ball.AssemblyLinearVelocity = Vector3.new(0, 0, 0)
	HomeGoalTrigger.Parent = workspace
	AwayGoalTrigger.Parent = workspace
	inGame.Value = true
	InIntermission.Value = false
	Status.Value = "In match"
	while true do
		task.wait()
		if HomeScore.Value == 3 then -- If home has scored 3 goals
			GiveTeamWins("Green", 1)
			Status.Value = "Green team wins the game"
			print("3 goals have been scored")
			wait(2)
			script.Parent.Intermission:Fire()
			break
		end
		if AwayScore.Value == 3 then
			GiveTeamWins("Red", 1)
			Status.Value = "Red wins the game"
			print("3 goals have been scored")
			wait(2)
			script.Parent.Intermission:Fire()
			break
		end
	end
end

script.Parent.StartGame.Event:Connect(StartingGame)

local function Intermission()
	HomeGoalTrigger.Parent = game.ServerStorage
	AwayGoalTrigger.Parent = game.ServerStorage
	inGame.Value = false
	InIntermission.Value = true
	HomeScore.Value = 0
	AwayScore.Value = 0
	for i = 5, 0, -1 do
		Status.Value = "Intermission: " .. i
		task.wait(1)
	end
	script.Parent.StartGame:Fire()
end

script.Parent.Intermission.Event:Connect(Intermission)

local function SendPlayersToSpawns()
	for _, player in pairs(game:GetService("Players"):GetPlayers()) do
		if player and player.Character then
			if player.Team == game.Teams.Home then
				player.Character:MoveTo(HomeSpawn.Position)
			end
			if player.Team == game.Teams.Away then
				player.Character:MoveTo(AwaySpawn.Position)
			end
		end
	end
end

script.Parent.TeleportTeamPlayers.Event:Connect(SendPlayersToSpawns)

local function NotEnoughPlayers()
	Status.Value = "Need 2 players to start the game."
	HomeGoalTrigger.Parent = game.ServerStorage
	AwayGoalTrigger.Parent = game.ServerStorage
	local toTween = StarterGui:WaitForChild("Start"):WaitForChild("NeedPlayersToStart")
	local tweenInfo = TweenInfo.new(0.3, Enum.EasingStyle.Quart, Enum.EasingDirection.InOut)
	local goal = {}
	goal.Position = UDim2.new(0.3, 0,0.798, 0)
	local Tween = TweenService:Create(toTween, tweenInfo, goal, {Position = goal.Position})
	Tween:Play()
end

NotEnoughPlayersEvent.Event:Connect(NotEnoughPlayers)

	if #plrs == 2 and InIntermission.Value == true then
		print("In intermission")
		script.Parent.Intermission:Fire()
	else
		script.Parent.NotEnoughPlayers:Fire()
	end

	if #plrs == 2 and inGame.Value == true then
		print(#plrs)
		script.Parent.StartGame:Fire()
		script.Parent.TeleportTeamPlayers:Fire()
	end
	

HomeGoalTrigger.Touched:Connect(function(hit)
	if hit.Name == "TPS" then
		AwayScore.Value = AwayScore.Value + 1
		hit.Position = Vector3.new(221.21, 1.944, -437.77)
		hit.AssemblyLinearVelocity = Vector3.new(0, 0, 0)
		hit.AssemblyAngularVelocity = Vector3.new(0, 0, 0)
	end
end)

AwayGoalTrigger.Touched:Connect(function(hit)
	if hit.Name == "TPS" then
		HomeScore.Value = HomeScore.Value + 1
		hit.Position = Vector3.new(221.21, 1.944, -437.77)
		hit.AssemblyLinearVelocity = Vector3.new(0, 0, 0)
		hit.AssemblyAngularVelocity = Vector3.new(0, 0, 0)
	end
end)

Any help is appreciated

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

repeat
	if #Players:GetPlayers() < 2 then Players.PlayerAdded:Wait() end
until #Players:GetPlayers() == 2
1 Like

Doesn’t seem to work where do you think I should put it?

Made a slight typo, should be good now.

1 Like