Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How can I get all players PlayerGui?

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

3 comments

0

Are you using a local or server script?

0

What's with the "2" comment...

0

The comment is so I know where something went right or wrong.

2 answers

✓ Accepted answer
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

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.

1 comment

0

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

This discussion is preserved as it was. New answers, comments, and votes are closed.