How to check when players leave?

Title explains it. How can I do that? I want to check it if a player(s) leave, and it’s under 3 players in a server.

Here is my script:

wait(1.5)
-- Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")
--// Varibles
local InRound = ReplicatedStorage.InRound
local RoundStatus = ReplicatedStorage.RoundStatus
local SendStandings = ReplicatedStorage.WinnerUI

while true do
	InRound.Value = false
	
	local Players = game:GetService("Players")

	if #Players:GetPlayers() < 3 then
		print("Not Enough Players to start game.")
		RoundStatus.Value = "Not enough players in server! Please wait till there is 3 players in the game."
		repeat wait() until #Players:GetPlayers() >= 3
		print("Enough players")
		RoundStatus.Value = "Ready to start Server!"
	else
		print("Enough players")
		RoundStatus.Value = "Ready to start Server!"
	end

	function getStandings()
		local standings = game.Players:GetPlayers()

		table.sort(standings, function(a,b)
			return a.RoundKills.Value > b.RoundKills.Value --change this to leaderstats.Kills, or whatever determines standings
		end)

		print(standings[1].Name.." got first place!")
		print(standings[2].Name.." got second place!")
		print(standings[3].Name.." got third place!")

		SendStandings:FireAllClients(standings)
		
		wait(3)
		standings[1].leaderstats:FindFirstChild("Points").Value += 150
		standings[1].leaderstats:FindFirstChild("Wins").Value += 1
		standings[2].leaderstats:FindFirstChild("Points").Value += 90
		standings[3].leaderstats:FindFirstChild("Points").Value += 55


		return standings
	end

	if RoundStatus.Value == "Ready to start Server!" then
		RoundStatus.Value = "Intermission"
		wait(30)
		RoundStatus.Value = "Ready to start!"
		wait(2)
		InRound.Value = true
		local timer = RoundStatus
		local minutes = 3
		local seconds = 0

		repeat
			game.ReplicatedStorage.InRound.Value = true
			if seconds <= 0 then
				minutes = minutes - 1
				seconds = 59
			else
				seconds = seconds - 1
			end
			if seconds < 10 then
				timer.Value = tostring(minutes)..":0"..tostring(seconds)
			else 
				timer.Value = tostring(minutes)..":"..tostring(seconds)
			end
			wait(1)
		until minutes <= 0 and seconds <= 0

		if minutes <= 0 and seconds <= 0 then
			RoundStatus.Value = "End of Game!"
			local standings = getStandings()
			wait(10)
			InRound.Value = false
			for i, v in pairs(Players:GetPlayers()) do
				v.RoundKills.Value = 0
			end
		end
	end
end

Help soon,

papahetfan

Instead of using resources on a while loop checking the number of players in the server, you could have a value that changes every time the Players.PlayerRemoving event fires.

game:GetService("Players").PlayerRemoving:Connect(function()	
---Script
end)

here this should work

or

game:GetService("Players").PlayerRemoving:Connect(--your function)