How to detect that 2 players have joined

Hello! Today I am trying to make a 1v1 system, that is why I created a place inside my game and that there is a maximum of 2 players per server. So in order to enable the 1v1 it is necessary that the 2 players enter a server to enable the fight, but I have searched everywhere and I don’t know how to do it.

while task.wait() do
if not game.Players:GetPlayers() >= 2 then
print("There aren't 2 people in game!")
else
print("2 or more people!")
--code here is 
end
end

Just some sample code for you, I didn’t format it because I wrote it here.
Good luck!

2 Likes

image
I get this error bro I don’t know why :frowning_face:

image

while task.wait() do
if not game.Players:GetPlayers() >= 2 then
print("There aren't 2 people in game!")
else
print("2 or more people!")
--code here 
end
end

Use that. Sorry, I have no brain.

1 Like

image
:pensive:
btw it’s a local script

It might be better to have a lobby place. Then pair up players in the lobby and send them to a reserved server.

How can take 2 players to reserve it

local players = 0
while task.wait() do
	for i, v in ipairs(game.Players:GetPlayers()) do
		players += 1
	end
	if players >= 2 then
		print("More than 2!")
--//code
		else warn("Less than 2!")
	end
end

As I said, I dont have a brain. Here you go. If you need to reset the players every time, move local players = 0 to line 3.

while task.wait() do
	if  #game.Players:GetPlayers() < 2 then
		print("There aren't 2 people in game!")
	else
		print("2 or more people!")
		--code here 
	end
end
1 Like

just do:

local PlayerService = game:GetService("Players")

PlayerService.PlayerAdded:Connect(function(player)
   if #PlayerService:GetPlayers() >= 2 then
      print("Enough players!") -- 2+ players
   else
      print("Waiting for players!") -- 0 players or 1 player
   end
end)

I suggest to use this instead of using a while wait loop.
Though I do suggest when you set up people for 1v1s to use a while loop to check if anyone else has to be set up for a 1v1.

While loops can be heavy for the server and take away performance instead only doing it each time a new player is added will reduce this.

Move
local players = 0
to line 2.

First 3 lines should now look like:

while task.wait() do
local players = 0
	for i, v in ipairs(game.Players:GetPlayers()) do

@MichaelAharon123 please read my previous post.

3 Likes

You just gather up 2 players and then request a reserve server id. Then you can use the teleport service to port both players to the reserve server for their 1v1 match. Otherwise you’ll end up with other players joining the server and possibly interfering with the match.

@pyxfluff @Emskipo @KJry_s @ZombieCrunchUK Thank you very much for your help, I know how to do it :smiley:

3 Likes

Better script:

local Players = game:GetService("Players")

local playerCount = Instance.new("IntValue")

local function updatePlayerCount()
    playerCount.Value = #Players:GetPlayers()
end

Players.PlayerAdded:Connect(function()
    updatePlayerCount()
end)

Players.PlayerRemoving:Connect(function()
    updatePlayerCount()
end)

playerCount.Changed:Connect(function()
    if playerCount.Value => 2 then
        -- do something that you need
    end
end)

Try

local count = 0
Players = game:GetService("Players")

for _, players in pairs(Players:GetPlayers()) do
count = count + 1
if count <= 2 then
print("There are exactly two players or only one player in the game.")
elseif count = >= 2 then
print("There are more than two players in the game")
end
end

Although this may be incorrect.

local rate_Per_Second = 0.5

game:GetService("RunService").Heartbeat:Connect(function()
    task.wait(rate_Per_Second)

    if game.Players:GetPlayers() ~= 2 then
        warn("System: Expected 2 players, got "..#game.Players:GetPlayers()..".")
    else
        -- Add code when the criteria is met.
    end
end)

why are people using loops. Use this! Give this man a solution.

local Players = game:GetService("Players")
Players.PlayerAdded:Wait()
Players.PlayerAdded:Wait()

The unorthodox approach.

It seems people have forgotten about the usefulness of the “repeat until” loop.

local Players = game:GetService("Players")
repeat task.wait() until #Players:GetPlayers() >= 2

and another implementation which avoids the overhead of calling the instance method “:GetPlayers()” on the player’s service object.

local Players = game:GetService("Players")
local PlayerCount = 0

Players.PlayerAdded:Connect(function(Player)
	PlayerCount += 1
	if PlayerCount >= 2 then
		--Do code.
	end
end)

Players.PlayerRemoving:Connect(function(Player)
	PlayerCount -= 1
end)
1 Like