I’m not that familiar with tables but for some strange reason, this just doesn’t work
game.Players.PlayerAdded:Connect(function(plr)
local function counterround(plrtable)
plrtable = {}
table.insert(plrtable, plr.Name)
for i,v in pairs(plrtable) do
if i < 2 then
warn("Not enough players")
else
print("More than one player!")
end
end
end
counterround()
end)
Basically, when there’s two players in a server, it still states that there is not enough players however because there should be two items in the table, it should state there is more than one player.
I’ve tried adding delays but nothing has worked
I don’t think you need to use a for loop to check if there are more than 2 instances in the table, you can just use #plrtable, which returns a number how many thing are there
if #plrtable < 2 then
warn("Not enough players")
else
print("More than one player!")
end
local function counterround(plrtable)
plrtable = {}
table.insert(plrtable, plr.Name)
for i,v in pairs(plrtable) do
if #plrtable < 2 then
warn("Not enough players")
else
print("More than one player!")
end
end
end
game.Players.PlayerAdded:Connect(function(plr)
counterround()
end)
as coolyoshi said, you don’t have to make a for loop to basically get the amount of players in a server, and we also need a PlayerRemoving event in case someone leaves the game:
local function onPlayerChanged(plr)
local i = #game.Players:GetChildren() -- Returns the amount of children game.Players has (amount of players)
if i < 2 then
warn("Not enough players")
else
print("More than one player!")
end
end
game.Players.PlayerAdded:Connect(onPlayerChanged) -- Runs the function when a player joins the game
game.Players.PlayerRemoving:Connect(onPlayerChanged) -- Runs the function when a player leaves the game (In case there are 2 players and someone leaves)
oh you are right i forgot the player removed is really special, if you implement a timer before the game starts chek if the player number is more than 1 then start the timer and when the timer goes to 0 check again how many players there are
I’m not sure what you’re saying but the topic creator (the guy who made this topic) wanted to check if there wasn’t only one player in a server, because i < 1 checks if there’s one player or more but if you use i < 2 it checks if there’s 2 players or more