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

My function is supposed to check for 2 players, but when i have two players it doesnt work?

local SpecTeam = game.Teams.Spectating:GetPlayers()
function CheckForPlayers()
		print "CheckForPlayers"
		while true do
			if #SpecTeam >= 2
				then InRound.Value = true
			end
		wait(0.5)
	end
end

I think the problem lies in checking the number of players on the team.

2 comments

0

Are you getting an error from this?

0

no

4 answers

The loop is unnecessary for a function that only checks if two players are in the team once. Remove the while true do and the end associated to it for your solution.

It is CheckForPlayers not WaitForPlayers. If you wanted to wait then you’d use a repeat until approach but do not yield with wait, use an event-based approach with PlayerAdded on the team to know when a player is added. It is much more efficient then using wait for yielding.

repeat
    game.Teams.Spectating.PlayerAdded:Wait()
until #plrs >= 2

1 comment

0

Very nice answer, I assumed the question differently.

The only answer I can think of is that you didn't actually call your function.

after the last end of that function write

CheckForPlayers()

5 comments

0

the print is printing, so the function is firing.

0

Ah ok, are there any errors though?

0

Ah ok, are there any errors though?

0

Oops, sorry for double comment

0

nope

Answer: You never updated the number of players during the loop / You did not give your loop an exit when the desired condition was met / You did not call the function to start it

Fix:

local SpecTeam = game.Teams.Spectating:GetPlayers()
function CheckForPlayers()
	SpecTeam = game.Teams.Spectating:GetPlayers() -- Updates player list
	while wait(0.5) do
		if #SpecTeam >= 2 then
			InRound.Value = true
			break -- This is used to exit the while loop
		end
	end
end
CheckForPlayers() -- Start the function

More explanation: When you initialized the variable SpecTeam you set it to the value of the current players in the session (0 players, assuming the script is loaded immediately when the game starts). And you did not update the value in your loop at all. Therefore SpecTeam will remain as an empty table indefinitely. In addition to this problem you didn't provide a way for the loop to exit when your desired outcome has been reached.

Note: If you're going to copy my code, please hit "View Source" to copy it correctly.

Optimization: (A better way to do what you're doing)

function CheckForPlayers()
	repeat wait(0.5)
	until #game.Teams.Spectating:GetPlayers() >= 2
	InRound.Value = true
	return
end
CheckForPlayers()

Try this :

local SpecTeam = 0
for i, plrs in pairs(game.Players:GetPlayers()) do
	if plrs then
		if plrs.Team == game:GetService("Teams").Spectators then
			SpecTeam = SpecTeam + 1
		end
	end
end
function CheckForPlayers()
        print "CheckForPlayers"
	if SpecTeam >= 2 then 
		InRound.Value = true
	end
        wait(0.5)
    end
end

CheckForPlayers()

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