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

How can I get all players PlayerGui?

Asked by 10 years ago

I want to make a script where if there are 2 or more players in the server, it will change the Gui's text at the top, and if there are less it will change it also, but whenever it gets to the line that changes the text, it returns nil. This is what I wrote:

NumOfPlayers = 2
gamewait = 10

while wait(2) do
    local topGui = game.Players:GetChildren('Player')
    wait(3)
    if game.Players.NumPlayers > NumOfPlayers then
        for i = 0, gamewait do -- 1
            topGui.PlayerGui.ScreenGui.TextLabel.Text = "Next Game Starts In:  " ..gamewait - i .." Seconds."
            print('1')
            wait(1)
        end
    else
        if topGui then -- 2
            topGui.PlayerGui.ScreenGui.TextLabel.Text = "There needs to be ".. NumOfPlayers - game.Players.NumPlayers .. " more player(s) to start the games."
            print('2') 
        else
            print('X-2')
        end
    end
end

0
Are you using a local or server script? jobro13 980 — 10y
0
What's with the "2" comment... OniiCh_n 410 — 10y
0
The comment is so I know where something went right or wrong. modMikey 0 — 10y

2 answers

Log in to vote
1
Answered by
Tkdriverx 514 Moderation Voter
10 years ago
local   NumOfPlayers    =   2
local   gamewait        =   10
local   timeLeft        =   gamewait

while true do
    if #game.Players:children() >= NumOfPlayers then
        timeLeft = timeLeft - wait() -- More accurate countdown; also will prevent crash.
    else
        wait() -- Prevents crash when there is not enough players.
        timeLeft = gamewait
    end

    for _, player in pairs(game.Players:players()) do
        if #game.Players:children() >= NumOfPlayers then
            player.PlayerGui.ScreenGui.TextLabel.Text = "Next Game Starts In:  " .. math.ceil(timeLeft) .." Seconds."
        else
            player.PlayerGui.ScreenGui.TextLabel.Text = "There needs to be " .. NumOfPlayers - #game.Players:children() .. " more player(s) to start the games."
        end
    end

    if timeLeft <= 0 then -- What happens when the timer ends.
        timeLeft = gamewait

    end
end
Ad
Log in to vote
0
Answered by
Mowblow 117
10 years ago

Here's a better method! Make a numbervalue in workspace called "NumOfPlayers".

local numofplayers = game.Workspace.NumOfPlayers.Value
local enoughplayers = false

game.Players.PlayerAdded:connect(function()
numofplayers = numofplayers + 1
end)

game.Players.PlayerRemoving:connect(function()
numofplayers = numofplayers - 1
end)

while true do 
if numofplayers >= 2  then 
enoughplayers = true
else 
enoughplayers = false
end
end

After that, you should make the code that checks if enoughplayers is equal to true, then makes people start the game. You might want a boolvalue saying if the game has started. This will eliminate a code running over and over if enoughplayers = true.

0
That wasn't reall y what I was asking but thanks anyway. :) modMikey 0 — 10y

Answer this question