Repeat Until Boolean Issue

Hey, developers!

I’m having some trouble trying to create a certain piece of code that continues once a countdown is over or once all of a specified Boolean value within the players is set to false.

	Countdown = 30

repeat wait(1)
	Countdown = Countdown - 1

	Timer.Value = Countdown
until Countdown <= 0 or #game.Players:GetPlayers().Values.Playing == true <= 0

Just so you’re aware, the countdown piece works fine until I add more criteria. It would be the #game.Players:GetPlayers().Values.Playing == true <= 0” part that is giving me trouble. This isn’t how you’re supposed to do it obviously but I hope I can get some help here!

2 Likes

What’s that? Do you want to know the number of players?

#game.Players:GetPlayers() <= 0

If not, no idea what that is.

1 Like

The function call game.Players:GetPlayers() simply returns a table of players. You cannot directly interact with it in the way you are trying to to get your playing status. Try something like this.

local Countdown = 30
local NoMorePlayers = true

local Players = game:GetService("Players")

repeat
	wait(1)
	Countdown = Countdown - 1
	Timer.Value = Countdown
	
	NoMorePlayers = true
	for _,Player in ipairs(Players:GetPlayers()) do
		if Player.Values.Playing.Value then
			NoMorePlayers = false
			break
		end
	end
until Countdown <= 0 or NoMorePlayers

This code block sets NoMorePlayers to true each iteration and then goes through all players and stops once it finds a player who is playing the game. If it the script finds a player who is playing, it sets NoMorePlayers to false and stops looking. This ensures the countdown will continue. If no players who are playing the game are found, NoMorePlayers remains true through the block and ultimately escapes the repeat loop.

This code also assumes that Player.Values.Playing is a BoolValue. If that is not the case, modification to the code will be required.

1 Like