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

How do I make this while true do check the numbers of players every second??


local players = game.Players:GetPlayers()
local _G players = {}
while true do
wait(1)
for i,v in pairs(players) do
table.insert(_G.players,v)	
end
wait(1)
print("hi")
wait(1)
print("yo")
wait(1)	
if _G.Players == 0 then
break	
end 
end



I am struggling to think of a way that I can check every second if the numbers of players is == 0 and it would make it break out of the while true do to give the winner names and other stuff. I cant get a clue on how to do it. Any help?

1 answer

First of all: tab your code properly.

local players = game.Players:GetPlayers()
local _G
players = {}

while true do
	wait(1)
	for i,v in pairs(players) do
		table.insert(_G.players,v)  
	end
	wait(1)
	print("hi")
	wait(1)
	print("yo")
	wait(1) 
	if _G.Players == 0 then
		break   
	end 
end

Notice that your definition of _G makes no sense at all. You shouldn't even use _G in your code, it's a bad practice for a lot of reasons.

You never change the players local variable, and you never empty the _G.Players (or _G.players as you also call it...) which also was never initialized.


I'm not sure what you're going for, because your code has many mistakes and makes no sense.


If you want to know if a list is empty, you can compare its length to 0. You get the length of list using #list:

if #playing == 0 then
	-- `playing` is an empty list
end

3 comments

0

How would I check it every second though?

0

how do you do anything every second?

0

while wait(1) do -- this stuff happens over and over with one second pauses end

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