Intermission system not working

hi, im trying to make it so if a second player joins, it will go into an intermission state. but its not really working when i tested it defaulting to the not enough player text instead. is there anything wrong with my code?

players = game.Players:GetChildren()
timer = game.ReplicatedStorage.Value.Value
playa = game.Players

playa.PlayerAdded:Connect(function()
if #players > 1 then
	print("intermission")
	repeat
		timer -= 1
	until timer == 0
else
	print("not enough players")
end
end)

you define players once, but never change it
players is always 1, because it runs once at the start of the server and then it never changes

oh how could i define it so it auto changes again and again (sorry im not that good at coding)

just redefine it in the repeat until loop

hmm how would that work? wouldnt redefining it in the loop have the same result? the server never checks through the loop

local players = game:GetService("Players")

while #players:GetPlayers() > 1 do
  -- logic
end

you coulkd put this in a function and calll it when needed

ah i see what if i want to make something happen if there arent enough players with while and end

There’s a few issues with this system. First, it does intermission every time a player joins, so if you have 2 players and another joins, it’ll enter the intermission loop again (unless you use debounce but I still wouldn’t recommend using PlayerAdded to handle that). Instead I would have a thread that constantly runs on a heartbeat loop, checking if the round can start. And like @notsad2ALT said, when you define players in the beginning, it gets all the players who are currently in the game and stores that into a new table, it can’t update itself and since this is at the very beginning of a server script it’ll ALWAYS be empty (server loads before any client does). So inside your intermission loop, you need to reassign players to game.Players:GetChildren() (or :GetPlayers()). Here’s a little sample I wrote that uses a thread and heartbeat. The doRound function yields the main thread and when it returns (as the round ends), the game will smoothly go back into the intermission loop and check that there are enough players to start the round.

local playerService = game:GetService("Players") -- personal preference i just like having this as a variable 
local runService = game:GetService("RunService") -- for heartbeat instead of while task.wait()
local players = {}
--local timer = game.ReplicatedStorage.Value.Value you don't really need timer value since you can honestly just use attributes instead

local function doRound()
	task.wait(5)
	warn("round end")
	return
end

task.spawn(function()
	while true do
		players = playerService:GetPlayers() -- also personal preference, :GetChildren() does work here but :GetPlayers() is better
		
		if #players>1 then
			task.wait(15) -- intermission time
			doRound()
		end
		
		runService.Heartbeat:Wait()
	end
end)

playerService.PlayerAdded:Connect(function()
	-- Not sure what you need this for now so I just left it 
end)

also please make your variables local!!!

gosh dayum
btw the player.added was just for detecting if there was 2 or more players. btw, whats the function for doRound() for?

The doRound function is supposed to handle the round and stop the intermission until the round ends, so if you wanted lets say a ‘last player alive’ type round, you would have it be something like

function doRound()
	local alive = playerService:GetPlayers()
	local lastAlive:Player
	while true and #alive>1 do
		runService.Heartbeat:Wait()
		alive = playerService:GetPlayers()
	end
	lastAlive = alive[1]
	warn(`{lastAlive.Name} won this round, ending round and returning to intermission`)
end

You don’t have to use that function and I probably wouldn’t, I’d use module scripts but it was just for example purposes

It’s because of this line:

Usually when doing this you do (or atleast thats what I do)

 :GetPlayers()

And each time a player joins it resets and does it again I would do this personally

local players = game.Players:GetPlayers()
local timer = game.ReplicatedStorage.Value.Value
local playersService = game:GetService("Players")

local intermissionTime = 10 --whatever number you want fr
while true do
  if #players > 1 then
      print("intermission")
      for i = intermissionTime, 0, -1 do
         timer = i
         task.wait(1)
      end
else
      print("Need more players")
  end
task.wait(0.2)
end

Hopefully this works I wrote all of this out on here so there may be typos

I also added some customizable time for intermission