Wall that only appears when there are a certain amount of people in the game

Hello! I would like to create a wall that only is visible and collide-able when there is a certain amount of players in the game.

e.g. The wall can only be collided with and is visible when there are 30 players or less in the game. If there are 31 players in the game then the wall disappears.

The issue I am having is that I can’t find any API that detects how many people are in a game.

I have tried to count players when they join, but this just caused the issue that it thought players who left were still in the game.

All help will be appreciated. :slight_smile:

1 Like
local function checkPlayerCount()
     if #game.Players:GetPlayers() <= 30 then
          wall.CanCollide = true
          wall.Transparency = 0
     else
          wall.CanColliide = false
          wall.Transparency = 1
     end
end
game.Players.PlayerAdded:Connect(checkPlayerCount)
game.Players.PlayerRemoving:Connect(checkPlayerCount)
1 Like

You could let a counter variable control this, then use the PlayerAdded & PlayerRemoving event to detect it.

local Players = game:GetService('Players')
local Counter = 0
local Start = 10 -- show them the wall if it's 10 or above
local End = 5 -- remove the wall if it's 5 or under

Players.PlayerAdded:Connect(function(Player)
	Counter += 1
	if Counter >= Start then
		-- show them the wall
	end
end)

Players.PlayerAdded:Connect(function(Player)
	Counter -= 1
	if Counter <= End then
		-- stop showing them the wall
	end
end)