How to make it check if there are 2 or more players to start the round

Hi! Recently, I have been making a round system script for my game, but I have run into an issue. The code I have created only checks if there is less than 2 players once as soon as a player joins the game.

The screenshot above shows what my problem is. It only checks if there is 2 players one time and nothing else. I’ve tried rewriting my script, look at some tutorials, and looked on the Developer Hub. My code is shown below:

-- / VARIABLES /

local IntermissionTimer = 30
local RoundTimer = 300
local RS = game:WaitForChild("ReplicatedStorage")
local MapsFolder = RS.Maps:GetChildren()
local Players = game.Players:GetChildren()
local RoundActive = script.RoundActive
local RoundStatus = script.RoundStatus

-- / CODE /

while true do
	
	if #Players < 2 then -- My attempt on making it check if there are less than 2 players
		while true do
			RoundStatus.Value = "Not enough players, requirement 2!"
			return
		end
	end
	
	for i = 10, 0, -1 do
		RoundStatus.Value = "Intermission "..i
		task.wait(1)
	end
	
	local RNGMap = MapsFolder[math.random(1, #MapsFolder)]
	local MapChosen = RNGMap:Clone()

	MapChosen.Parent = game.Workspace
	RoundStatus.Value = "Map Chosen: "..MapChosen.Name
	
	task.wait(3)
	
	for i, Player in pairs(game.Players:GetPlayers()) do
		
		local Character = Player.Character
		
		if Player.PlayerScripts.IsPlaying == false then
			print("Not Playing")
		else
			if Character then
				local HumRootPart = Character.HumanoidRootPart
				
				HumRootPart.Position = MapChosen.TeleportPoint.Position
			end
		end
		
	end
	
	for i = 300, 0, -1 do
		RoundStatus.Value = "Game: "..i
		task.wait(1)
	end
	
	for i, Player in pairs(game.Players:GetPlayers()) do

		local Character = Player.Character

		if Character then

			local HumRootPart = Character.HumanoidRootPart

			HumRootPart.Position = Vector3.new(5.76, 77.4, -25.73)
		end

	end
end
2 Likes

Two things.
One you are never refreshing your Players variable, it is always staying constantly 0 I believe.

Fix this by doing

local players = game.Players:GetPlayers()

notice how I did GetPlayers instead of GetChildren that is the second thing, depends if anything gets inside the players in game then that’s bad so just do getplayers.

hope this helps!!!

Please

	if #Players < 2 then -- My attempt on making it check if there are less than 2 players
		while true do
			RoundStatus.Value = "Not enough players, requirement 2!"
			return
		end
	end

This isn’t the best thing
It could be optimised but eh.
try this

while task.wait(0.1) do
     Players = game.Players:GetPlayers()
     if #Players < 2 then RoundStatus.Value = "Not enough players, requirement 2! else break end
end

You can use the game:GetService(“Players”).PlayerAdded event to detect when a player joins.

If you want the game to continuously loop then you can turn your code into a function and call that function whenever a player joins. Then you’d have to check if #game:GetService(“Players”):GetPlayers() gives 2+ players.

You’d also have to make sure that whenever a player joins a round isn’t already in progress. Then whenever you wanna start a new round, you can just call that function again.

Thanks for the tip, it worked!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.