How would I make an AFK system?

I have this round system script that I originally got from YouTube but since then I’ve modified it. The problem is just that how would I make an AFK system. I think I’ve made it where the afk player won’t teleport to the map, but if theres only 2 players and one of them is afk, the round still starts even though one of them is afk so how could I make it where the round doesnt start if that happens?

-------------------------------------[AUTHOR: reccur]--------------------------------


local intermission = 10
local roundLength = 360

local inRound = game.ReplicatedStorage.Values.InRound
local staus = game.ReplicatedStorage.Values.Status
local maps = game.ServerStorage.Maps:GetChildren()


local playingTeam = game.Teams.Playing
local lobbyTeam = game.Teams.Spectating
local potatoTeam = game.Teams.Potato

local defsky = game.ReplicatedStorage.Skies["Realistic Sky"]:Clone()
local crossroadsky = game.ReplicatedStorage.Skies.CrossroadsSky:Clone()


game.Players.PlayerAdded:Connect(function(p)
	p:LoadCharacter()
end)

inRound.Changed:Connect(function()
	
	if inRound.Value == true then
		
		for i, plr in pairs(game.Players:GetChildren()) do
			if plr.Character.Values.isAFK.Value == false then
				local char = plr.Character
				local humanRoot = char:WaitForChild("HumanoidRootPart")

				plr.Team = playingTeam

				char:WaitForChild("Humanoid").Died:Connect(function()

					plr.Team = lobbyTeam


				end)

			end
		end	
		
	else
		
		for i, plr in pairs(game.Players:GetChildren()) do

			local char = plr.Character
			local humanRoot = char:WaitForChild("HumanoidRootPart")

			humanRoot.CFrame = game.Workspace.Lobby.lobbySpawn.CFrame
			
			plr.Team = lobbyTeam
			
		end
	end	
end)

game.ReplicatedStorage.Audios.Music.Intermission:Play()

local function round()	
	while true do
		local requiredPlayers = 1
		
		repeat
			wait(1)
			staus.Value = "At least ".. requiredPlayers.." players are needed to start a round"
			
		until #game.Players:GetChildren() >= requiredPlayers
		
		game.Lighting.Ambient = Color3.fromRGB(70, 70, 70)
		game.Lighting.TimeOfDay = 12
		script.Parent.RandomPotatoGiver.Enabled = false
		script.Parent.FallingRocksEvent.Enabled = false
		game.ReplicatedStorage.Audios.Music.Game:Stop()
		game.ReplicatedStorage.Audios.PotatoWarning:Stop()
		game.ReplicatedStorage.Audios.Earthquake:Stop()
		game.ReplicatedStorage.Audios.PotatoWarning:Stop()
		if not game.ReplicatedStorage.Audios.Music.Intermission.IsPlaying then
			game.ReplicatedStorage.Audios.Music.Intermission:Play()
		end
		game.ReplicatedStorage.Remotes.Camera.BackToPlayerCam:FireAllClients()
		game.ReplicatedStorage.Remotes.Camera.SpeedlinesOFF:FireAllClients()
		defsky.Parent = game.Lighting
		
		inRound.Value = false
		
		local oldwinparticles = game.Workspace:FindFirstChild("WinnerParticle")
		
		if oldwinparticles then
			oldwinparticles:Destroy()
		end
		
		for _, plr in pairs(game.Players:GetChildren()) do
			if plr.Character.Values.isAFK.Value == false then
				plr:LoadCharacter()
			end
		end
		
		for i = intermission, 0, -1 do
			-- Calculate minutes and seconds
			local minutes = math.floor(i / 60)
			local seconds = i % 60  -- This will give the remainder (seconds)

			-- Format the countdown as "Minutes:Seconds"
			local timeDisplay = string.format("%02d:%02d", minutes, seconds)

			-- Update the status display with the formatted countdown
			staus.Value = "Intermission: (" .. timeDisplay .. ")"

			wait(1)
		end

		staus.Value = "Choosing Map"
		
		local ChosenMap = maps[math.random(1, #maps)]:Clone()
		local Spawns = ChosenMap:FindFirstChild("Spawns"):GetChildren()
		
		wait(5)
		
		ChosenMap.Parent = workspace
		staus.Value = "Chosen map is "..ChosenMap.Name.."!"
		
		wait(2)
	
		if ChosenMap.Name == "Death Arena" then
			game.Lighting.Ambient = Color3.fromRGB(125, 42, 0)
			game.Lighting.TimeOfDay = 3
		end
		
		--if ChosenMap.Name == "Crossroads" then
		--	crossroadsky.Parent = game.Lighting
		--end
		
		for i, plr in pairs(game.Players:GetChildren()) do
			local char = plr.Character
			local RandomSpawn = Spawns[math.random(1, #Spawns)]
			if char.Values.isAFK.Value == false then
				if char and char:FindFirstChild("Humanoid") then
					char.HumanoidRootPart.CFrame = RandomSpawn.CFrame
				end
			else
				
			end
		end
		
		inRound.Value = true
		wait(1)
		script.Parent.RandomPotatoGiver.Enabled = true
		game.ReplicatedStorage.Audios.Music.Intermission:Stop()
		game.ReplicatedStorage.Audios.Music.Game:Play()
		

		for i = roundLength, 0, -1 do
			-- Calculate minutes and seconds
			local minutes = math.floor(i / 60)
			local seconds = i % 60  -- This will give the remainder, i.e., seconds

			-- Format the countdown as "Minutes:Seconds"
			local timeDisplay = string.format("%02d:%02d", minutes, seconds)

			-- Update the status display with the formatted countdown
			staus.Value = "Game will end in " .. timeDisplay

			local playing = {}

			for _, plr in pairs(game.Players:GetChildren()) do
				if plr.Team.Name == "Playing" or plr.Team.Name == "Potato" then
					table.insert(playing, plr.Name)
				end
			end
			
			if ChosenMap.Name == "Death Arena" and i == 350 then
				game.ReplicatedStorage.Audios.Danger:Play()
				game.ReplicatedStorage.Audios.Earthquake:Play()
				game.ReplicatedStorage.Remotes.Minigames.StartEarthquake:FireAllClients()
				script.Parent.FallingRocksEvent.Enabled = true
			end
			
			if ChosenMap.Name == "Towering Islands" and i == 350 then
				game.ReplicatedStorage.Audios.Danger:Play()
				game.ReplicatedStorage.Remotes.Minigames.StartBotSpawning:FireAllClients()
				script.Parent.PotatoBotsSpawn.Enabled = true
			end

			if #playing == 0 then
				staus.Value = "Everyone Has Died"
				script.Parent.RandomPotatoGiver.Enabled = false
				game.ReplicatedStorage.Audios.Music.Intermission:Stop()
				game.ReplicatedStorage.Audios.Music.Game:Stop()
				game.ReplicatedStorage.Audios.EveryoneLost:Play()
				game.ReplicatedStorage.Remotes.Camera.CameraShake:FireAllClients(1, 0.6)
				if ChosenMap.Name == "Towering Islands" then
					script.Parent.PotatoBotsSpawn.Enabled = false
					ChosenMap.PotatoBots:Destroy()
				end
				wait(3)
				break
			end

			if #playing == 1 then
				staus.Value = playing[1] .. " Has Won The Game!"
				if ChosenMap.Name == "Towering Islands" then
					script.Parent.PotatoBotsSpawn.Enabled = false
					ChosenMap.PotatoBots:Destroy()
				end
				for _, plr in pairs(game.Players:GetChildren()) do
					if playing[1] == plr.Name then
						local winanim = Instance.new("Animation")
						winanim.AnimationId = ""..plr.Character.Values.EmoteSelected.Value
						local dance = plr.Character.Humanoid.Animator:LoadAnimation(winanim)
						local winparticles = game.ReplicatedStorage.Models.WinnerParticle:Clone()
						if plr.Character:FindFirstChild("HotPotato") then
							plr.Character.HotPotato:Destroy()
							plr.Character:WaitForChild("Highlight"):Destroy()
							game.ReplicatedStorage.Remotes.Camera.SpeedlinesOFF:FireClient(plr)
						end
						script.Parent.RandomPotatoGiver.Enabled = false
						winparticles.Parent = game.Workspace
						winparticles.Position = plr.Character.Head.Position + Vector3.new(0, 10, 0)
						plr.leaderstats.Wins.Value += 1
						plr.leaderstats.Cash.Value += 220
						dance:Play()
						game.ReplicatedStorage.Audios.Music.Intermission:Stop()
						game.ReplicatedStorage.Audios.Music.Game:Stop()
						game.ReplicatedStorage.Audios.WinnerAmbience:Play()
						game.ReplicatedStorage.Audios.Winner:Play()
					end
				end

				wait(5)
				break
			end

			wait(1)
		end

		wait(3)
		ChosenMap:Destroy()
	end
end


spawn(round)
1 Like
local intermission = 10
local roundLength = 360

local inRound = game.ReplicatedStorage.Values.InRound
local status = game.ReplicatedStorage.Values.Status
local maps = game.ServerStorage.Maps:GetChildren()

local playingTeam = game.Teams.Playing
local lobbyTeam = game.Teams.Spectating
local potatoTeam = game.Teams.Potato

local defsky = game.ReplicatedStorage.Skies["Realistic Sky"]:Clone()
local crossroadsky = game.ReplicatedStorage.Skies.CrossroadsSky:Clone()

game.Players.PlayerAdded:Connect(function(p)
    p:LoadCharacter()
end)

local function getActivePlayers()
    local activePlayers = 0
    for _, plr in pairs(game.Players:GetChildren()) do
        if plr.Character and plr.Character:FindFirstChild("Values") and not plr.Character.Values.isAFK.Value then
            activePlayers += 1
        end
    end
    return activePlayers
end

inRound.Changed:Connect(function()
    if inRound.Value == true then
        for _, plr in pairs(game.Players:GetChildren()) do
            if plr.Character.Values.isAFK.Value == false then
                local char = plr.Character
                local humanRoot = char:WaitForChild("HumanoidRootPart")
                plr.Team = playingTeam
                char:WaitForChild("Humanoid").Died:Connect(function()
                    plr.Team = lobbyTeam
                end)
            end
        end    
    else
        for _, plr in pairs(game.Players:GetChildren()) do
            local char = plr.Character
            local humanRoot = char:WaitForChild("HumanoidRootPart")
            humanRoot.CFrame = game.Workspace.Lobby.lobbySpawn.CFrame
            plr.Team = lobbyTeam
        end
    end    
end)

game.ReplicatedStorage.Audios.Music.Intermission:Play()

local function round()
    while true do
        local requiredPlayers = 2
        
        repeat
            wait(1)
            status.Value = "At least " .. requiredPlayers .. " active players are needed to start a round"
        until getActivePlayers() >= requiredPlayers
        
        game.Lighting.Ambient = Color3.fromRGB(70, 70, 70)
        game.Lighting.TimeOfDay = 12
        script.Parent.RandomPotatoGiver.Enabled = false
        script.Parent.FallingRocksEvent.Enabled = false
        game.ReplicatedStorage.Audios.Music.Game:Stop()
        game.ReplicatedStorage.Audios.PotatoWarning:Stop()
        game.ReplicatedStorage.Audios.Earthquake:Stop()
        if not game.ReplicatedStorage.Audios.Music.Intermission.IsPlaying then
            game.ReplicatedStorage.Audios.Music.Intermission:Play()
        end
        game.ReplicatedStorage.Remotes.Camera.BackToPlayerCam:FireAllClients()
        game.ReplicatedStorage.Remotes.Camera.SpeedlinesOFF:FireAllClients()
        defsky.Parent = game.Lighting
        
        inRound.Value = false
        
        for _, plr in pairs(game.Players:GetChildren()) do
            if plr.Character.Values.isAFK.Value == false then
                plr:LoadCharacter()
            end
        end
        
        for i = intermission, 0, -1 do
            status.Value = "Intermission: (" .. string.format("%02d:%02d", math.floor(i / 60), i % 60) .. ")"
            wait(1)
        end
        
        status.Value = "Choosing Map"
        local ChosenMap = maps[math.random(1, #maps)]:Clone()
        local Spawns = ChosenMap:FindFirstChild("Spawns"):GetChildren()
        wait(5)
        ChosenMap.Parent = workspace
        status.Value = "Chosen map is "..ChosenMap.Name.."!"
        wait(2)

        for _, plr in pairs(game.Players:GetChildren()) do
            local char = plr.Character
            local RandomSpawn = Spawns[math.random(1, #Spawns)]
            if char.Values.isAFK.Value == false then
                if char and char:FindFirstChild("Humanoid") then
                    char.HumanoidRootPart.CFrame = RandomSpawn.CFrame
                end
            end
        end

        inRound.Value = true
        wait(1)
        script.Parent.RandomPotatoGiver.Enabled = true
        game.ReplicatedStorage.Audios.Music.Intermission:Stop()
        game.ReplicatedStorage.Audios.Music.Game:Play()

        for i = roundLength, 0, -1 do
            status.Value = "Game will end in " .. string.format("%02d:%02d", math.floor(i / 60), i % 60)
            
            local playing = {}
            for _, plr in pairs(game.Players:GetChildren()) do
                if plr.Team.Name == "Playing" or plr.Team.Name == "Potato" then
                    table.insert(playing, plr.Name)
                end
            end
            
            if #playing == 0 then
                status.Value = "Everyone Has Died"
                script.Parent.RandomPotatoGiver.Enabled = false
                game.ReplicatedStorage.Audios.Music.Game:Stop()
                game.ReplicatedStorage.Audios.EveryoneLost:Play()
                game.ReplicatedStorage.Remotes.Camera.CameraShake:FireAllClients(1, 0.6)
                wait(3)
                break
            end
            
            if #playing == 1 then
                status.Value = playing[1] .. " Has Won The Game!"
                script.Parent.RandomPotatoGiver.Enabled = false
                game.ReplicatedStorage.Audios.Music.Game:Stop()
                game.ReplicatedStorage.Audios.Winner:Play()
                wait(5)
                break
            end
            wait(1)
        end
        wait(3)
        ChosenMap:Destroy()
    end
end

spawn(round)

what did you do to the script, like what did u add or change?

until #game.Players:GetChildren() >= requiredPlayers replaced with `local function getActivePlayers()
local activePlayers = 0
for _, plr in pairs(game.Players:GetChildren()) do
if plr.Character and plr.Character:FindFirstChild(“Values”) and not plr.Character.Values.isAFK.Value then
activePlayers += 1
end
end
return activePlayers
end

repeat
wait(1)
staus.Value = “At least " … requiredPlayers … " active players are needed to start a round”
until getActivePlayers() >= requiredPlayers
`

ohh i see what you did know let me test it

1 Like