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

Why wont this break from the while true do??

local players = game.Players:GetChildren() 
local h = script.Value
local playertable = {}
while true do
	wait(3)
	if game.Players.NumPlayers == 1 then 
	print("Gettingpairs")
	for i,v in pairs(game.Players:GetPlayers()) do
		if v and v.Character then 
			table.insert(playertable,v)
		end
	print(#playertable)
	if #playertable == 1 then
	print("Break out of the whiletruedo")	
	break
	else	
	print("keep going")	
	end
	end
	end
end	

print("yo,game over")

Why will it not break from the while true do when I have 1 player, it just keeps repeating when I want it to break when it only has 1 Player in the table. Any help?

4 comments

1

You never clear the playertable

0

What

1

Tab your code properly.

0

I Thought this was scripting helpers, where you can help people with their code. If you could please help me learn tabbing and show me the problem that would be great!

1 answer

Tab your code properly. The Glossary has this covered.

See how there's a big space at the beginning of lines 5 through 20 in your code?

That shows that all of that code is grouped inside the while on line 4.

You should do that for everything that groups code together -- then ... else ... end, do ... end, etc.

This makes it possible for you to understand what code happens when. Without it, you have to spend way too much time figuring out which ends go where and which conditions matter.

local players = game.Players:GetChildren() 
local h = script.Value
local playertable = {}
while true do
	wait(3)
	if game.Players.NumPlayers == 1 then 
		print("Gettingpairs")
		for i,v in pairs(game.Players:GetPlayers()) do
			if v and v.Character then 
				table.insert(playertable,v)
			end
			print(#playertable)
			if #playertable == 1 then
				print("Break out of the whiletruedo")   
				break
			else
				print("keep going") 
			end
		end
	end
end 

print("yo,game over")

It is clear that this is not correct.

  • lines 12 .. 18 have nothing to do with i or v, but they're inside the for loop. They probably belong after.
  • you never use players defined at the top (it's useless)

You only ever add things to playertable . You never remove. That means it's just going to grow. You probably want to just filter down the currently running players; it should be initialized to {} inside the loop.

A name like "playertable" isn't that informative. Normally you'd just use a plural like players instead of including the fluff word "table"

Use meaningful names. I don't know what a v is. You don't use i and it also doesn't mean anything.

while true do
	wait(3)
	if game.Players.NumPlayers == 1 then 
		print("Gettingpairs")
		local players = {}
		for _, player in pairs(game.Players:GetPlayers()) do
			if player and player.Character then 
				table.insert(players, player)
			end
		end
		print(#players)
		if #players == 1 then
			print("Break out of the whiletruedo")   
			break
		else
			print("keep going") 
		end
	end
end 

print("yo,game over")

I'm not sure what the point of the game.Players.NumPlayer == 1 check is. It seems superfluous.

Because your code originally didn't make sense, and because it has no comments or explanation from you, I'm not sure what your intent is.

2 comments

0

I want it to see if there is only 1 player in the table, break out of the while true do loop.

0

The final chunk of code does that, there's just a superfluous if in it.

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