Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Is there a problem with text output for all players?

Asked by 6 years ago

local players = game:GetService("Players") local needtostart = 2 while wait() do local allplayers = players:GetChildren() for i,v in pairs(allplayers) do if i < needtostart then v:WaitForChild("PlayerGui"):WaitForChild("TopGui").MainFrame.ReadyInfo.Text = "Not enough players" else v:WaitForChild("PlayerGui"):WaitForChild("TopGui").MainFrame.ReadyInfo.Text = "Enough players" end end end

Why is the text displayed for only one player when I run the test? The script is in Workspace

1 answer

Log in to vote
0
Answered by
CootKitty 311 Moderation Voter
6 years ago

The problem is with the conditional statement.

Let's take a closer look with an example:

local tableOfPlayers = { -- pretend these are players and not strings
    "Player1",
    "Player2",
    "Player4" -- player 3 had sick day
}

for index, player in pairs(tableOfPlayers) do
    print(index, player)
end

-- OUTPUT:
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 = { -- pretend these are players and not strings
    "Player1",
    "Player2",
    "Player4" -- player 3 had sick day
}

local neededPlayers = 2

for index, player in pairs(tableOfPlayers) do
    if i < neededPlayers then
        print("Not enough players")
    else
        print(index, player)
    end
end

-- OUTPUT:
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 Script in PlayerGui
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() -- update when they join

game.Players.PlayerAdded:Connect(function()
    update() -- update when new player joins
end)

game.Players.PlayerRemoving:Connect(function()
    update() -- update when player leaves
end)
Ad

Answer this question