How would I improve this script?

Title - I just want to make this script more reliable. In its current state, it doesn’t track when a players team is updated like I wanted it to. There are no errors in the console and it does track the amount of players when a user first joins, but afterwards it stops counting. I’ve tried fiddling with it, adding the “while do” loop and a few waits, but I can’t exactly figure out what I’m doing wrong.

local players = game:GetService("Players")
local RS = game:GetService("ReplicatedStorage")

local status = RS:WaitForChild("Status")

roundTime = 50
inter = 15

canStart = false
roundStarted = false
requiredPlayers = 2

local function roundStart()
	roundStarted = true
	
	wait(inter)
	
	local activePlayers = {}
	status.Value = "Game starting shortly!"
	for _, p in pairs (players:GetPlayers()) do
		if p.Team == "Ready" then
			activePlayers.Insert(p)
			p.Team = "In-Game"
		end
	end
	
	-- blah blah blah, extra round code here lol
end

if canStart == false and roundStarted == false then
	while canStart == false and roundStarted == false do
		local plrs = players:GetPlayers()
		if #plrs >= requiredPlayers then
			local activePlayers = {}

			for _, p in pairs (players:GetPlayers()) do
				if p.Team == "Ready" then
					activePlayers.Insert(p)
				end
				if #activePlayers >= requiredPlayers then
					canStart = true
					roundStart()
				else
					canStart = false
					roundStarted = false
					wait(1)
					status.Value = "Needs at least "..requiredPlayers - #activePlayers.." more player[s]."
				end
				
			end
		else
			canStart = false
			roundStarted = false
			local activePlayers = {}
			for _, p in pairs (players:GetPlayers()) do
				if p.Team == "Ready" then
					activePlayers.Insert(p)
				end
			end
			wait(1)
			status.Value = "Needs at least "..requiredPlayers - #activePlayers.." more player[s]."
		end
	end
end

Do go easy on me heh, I’m not extremely familiar with coding and can really only do the most bare-bones functions.

I’m not sure what your issue is. Can you try to explain it more?

In a nutshell, the script is supposed to loop through the players and figure out what team they’re on. It’s supposed to do this until it finds enough players on the specific team - however, it doesn’t seem to be detecting a players team switching. I apologize if my wording was vague in the initial post.

I dont recommend using a while loop for this especially if this is a server script.
You can use players.PlayerAdded:Connect(function(Player) and players.PlayerRemoved:Connect(function(Player) and then make a table named something like: PlayerList = {} then simply insert and remove from the table on player joining and leaving. From this you can get the TotalPlayers from #PlayerList . Additionally if you want to store the team in the dictionary too you simply need to store it like:

PlayerList = {
["SirAphelion"] = "Lobby"
}

Or you can use simple number like [1] = {Name = “SirAphelion”, Team = “Lobby”}

To update the players team in the dictionary for the first version you can simply put in the PlayerAdded function:

Player:GetPropertyChangedSignal("Team"):Connect(function()
     PlayerList[Player.Name] = Player.Team.Name
end)

I see, is there a specific reason you don’t recommend using while loops for this?

Kind of unnecessary to use a while loop for it that’s my only reason why, it’ll work with one just it will work without one too.
I understand you did it so it all works with your round loading so