Function if players are greater than 2

I’m trying to call a function when there are more 2 or more players in the server. For some reason, the output is “Not enough players! (0/2)”. This is my code.

function RoundTimer()
	while wait() do
		for i = IntermissionLength,0,-1 do
			InRound.Value = false
			Status.Value = "Intermission ("..i.." seconds left)"
		end
		for i = RoundLength,1,-1 do
			InRound.Value = true
			Status.Value = "In game! ("..i.." seconds left)"
		end
	end
end

if #Players:GetPlayers() >= 1 or Players:FindFirstChild("zCrxtix") then
		spawn(RoundTimer)
else
	Status.Value = "Not enough players! ("..tostring(#Players:GetPlayers()).."/2)"
end

Try this:

( you didn’t added game.Players )

Code
 local Players = game.Players    
    function RoundTimer()
        	while wait() do
        		for i = IntermissionLength,0,-1 do
        			InRound.Value = false
        			Status.Value = "Intermission ("..i.." seconds left)"
        		end
        		for i = RoundLength,1,-1 do
        			InRound.Value = true
        			Status.Value = "In game! ("..i.." seconds left)"
        		end
        	end
        end

        if #Players:GetPlayers() >= 1 or Players:FindFirstChild("zCrxtix") then
        		spawn(RoundTimer)
        else
        	Status.Value = "Not enough players! ("..tostring(#Players:GetPlayers()).."/2)"
        end

I did. It’s in a variable, I just didn’t include it here.

Imma remake your script wait… ( )

This is because you are only calling this once. Have it in a while loop and check if every second:

while wait(1) do
    if #Players:GetPlayers() >= 1 or Players:FindFirstChild("zCrxtix") then
        spawn(RoundTimer)
    else
        Status.Value = "Not enough players! ("..tostring(#Players:GetPlayers()).."/2)"
    end
end

can you show your full script? with everything

local Players = game.Players
local Status = game.ReplicatedStorage.Status
local InRound = game.ReplicatedStorage.InRound
local RoundLength = 40
local IntermissionLength = 5
local TPs = game.Workspace.Teleports:GetChildren()
local ChosenTeleporter = math.random(1, #TPs)
local Player = Players:GetPlayers()


InRound.Changed:Connect(function()
	if InRound.Value == true then
		for _, player in pairs(Players:GetPlayers()) do 
			player.Character.HumanoidRootPart.Position = game.Workspace.Teleports[tostring(ChosenTeleporter)].Position + Vector3.new(0,3,0)
		end
			local Sword = game.ServerStorage.ClassicSword

			for _, player in pairs(Players:GetPlayers()) do
				wait(2)
			Sword:Clone().Parent = player.Backpack
			end
	else
		for _, player in pairs(game.Players:GetChildren()) do
			player:LoadCharacter()
		end
		
	end
end)

function RoundTimer()
	while wait() do
		for i = IntermissionLength,0,-1 do
			InRound.Value = false
			Status.Value = "Intermission ("..i.." seconds left)"
		end
		for i = RoundLength,1,-1 do
			InRound.Value = true
			Status.Value = "In game! ("..i.." seconds left)"
		end
	end
end

while wait(1) do
	if #Players:GetPlayers() >= 1 or Players:FindFirstChild("zCrxtix") then
		spawn(RoundTimer)
	else
		Status.Value = "Not enough players! ("..tostring(#Players:GetPlayers()).."/2)"
	end
end

Now it just breaks. I’m flying all over the place and getting respawned.

while wait() do
	local plr = game.Players:GetPlayers()

	if #plr >= 2 then
		print("STARTED")
	elseif #plr <= 1 then
			print("NOT ENOUGH PLAYERS")
		end
	end
end
1 Like

Thank you. It prints 1/2 now instead of 0/2 like it did before.

No problem dude! happy deving!