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.
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
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()
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()