The problem is with the conditional statement.
Let's take a closer look with an example:
local tableOfPlayers = {
"Player1",
"Player2",
"Player4"
}
for index, player in pairs(tableOfPlayers) do
print(index, player)
end
1, Player1
2, Player2
3, Player4
Index, or in your case i, is simply the place in the table at which the value is indexed. So, if we add a conditional like in your case:
local tableOfPlayers = {
"Player1",
"Player2",
"Player4"
}
local neededPlayers = 2
for index, player in pairs(tableOfPlayers) do
if i < neededPlayers then
print("Not enough players")
else
print(index, player)
end
end
Not enough players
Not enough players
3, Player4
See? Even though there are enough players, it doesn't check if there are, only if the index of some players is higher than needed to start. To fix this, we change the conditional.
Fixed code
Here's an example of how you could be able to fix your code:
local players = game:GetService("Players")
local needtostart = 2
players.PlayerAdded:Connect(function(plr)
if #players:GetPlayers()>=needtostart then
for _, plr in pairs(players:GetPlayers()) do
plr:WaitForChild("PlayerGui"):WaitForChild("TopGui").MainFrame.ReadyInfo.Text = "Enough players"
end
else
for _, plr in pairs(players:GetPlayers()) do
plr:WaitForChild("PlayerGui"):WaitForChild("TopGui").MainFrame.ReadyInfo.Text = "Not enough players"
end
end
end)
Don't use this yet.
But, if FilteringEnabled is on like it should be, the server can't see GUIs in PlayerGui which it did not create. So, you'll either need to use remote events or clone the GUIs into PlayerGui manually. I suggest Remotes. I just used a local script though.
Actually correct example:
You can just use a LocalScript in the end.
local plr = game.Players.LocalPlayer
local gui = plr:WaitForChild("PlayerGui"):WaitForChild("TopGui").MainFrame.ReadyInfo
local neededPlayers = 2
local function update()
if(game.Players.NumPlayers >= neededPlayers)then
gui.Text = "Enough players."
else
gui.Text = "Not enough players."
end
end
update()
game.Players.PlayerAdded:Connect(function()
update()
end)
game.Players.PlayerRemoving:Connect(function()
update()
end)