How to make only 5 Players Allowed in a Certain Area

1.I would like to be able to achieve a zone where only 5 people are allowed in that certain area. If there is an extra player that wants to be there, they will be teleported back to spawn until someone leaves the zone or leaves the game!

  1. I already have made a solution of using a zone that will count how many players are in that certain area. When the player goes into the zone the variable I made called playercount adds one to its value and when they leave it subtracts one to the playercount value. It also says their name when they lave

  2. I just need to find a way to be able to define the 5th Player in the area. Would I do this by doing Player5 = playercount.Value.5?
    Or would I use another function and define all players in the area and make more functions?

local ZonePlus = require(4664437268)
local ZoneService = require(ZonePlus.ZoneService)


-- Setup zone
local group = workspace.SafeZone2
local zone = ZoneService:createZone("SafeZone2", group, 0)
local safecheck = false
local playercount = 0
local player = game.Players.LocalPlayer
local table = {}

   
   


-- Create a safe zone by listening for players entering and leaving the zone
local safeZoneCheckInterval = 0.2
local forceFieldName = "PineapplesForLife"
local forceFieldTemplate = Instance.new("ForceField")
forceFieldTemplate.Name = forceFieldName

local connectionAdded = zone.playerAdded:Connect(function(player)
   
   safecheck = true
   if safecheck == true then
   	playercount = playercount + 1
   	print(playercount)
   	print(player.Name)
   	
   
   	
   end
   
   local char = player.Character
   local forceField = char and char:FindFirstChild(forceFieldName)
   if not forceField then
   	forceField = forceFieldTemplate:Clone()
   	forceField.Parent = char
   	
   end
end)
local connectionRemoving = zone.playerRemoving:Connect(function(player)
   safecheck = false
   if safecheck == false then
   	playercount = playercount - 1
   	print(playercount)
   	print(player.Name)
   end
   
   local char = player.Character
   local forceField = char and char:FindFirstChild(forceFieldName)
   if forceField then
   	forceField:Destroy()
   
   	end
   
end)

zone:initLoop(safeZoneCheckInterval)
5 Likes

I think this should be in Help and feedback - Scripting Support

2 Likes

i’ve made something similar to what you want, you can use a value to check how many players are in the area. When they go in the area for example you can use a touched event and increase the value of the players by one. You can use if statements to check if the value is the max amount of players you want or not, if it’s max change the players HumanoidRootPart CFrame to a parts CFrame that is at the spawn area

2 Likes

Yes my script is working right now and checks how many players are in the zone, but it will not move the fifth player who joins back to spawn. I want only five players allowed in the zone and if another person joins like the sixth person it will teleport them back to spawn. RIght now my script only just detects how many people. I do not know how to define the sixth player. How can I make a table?

1 Like

I changed it to Help and feedback - Scripting Support
Thanks!

3 Likes

I updated your connectionAdded function. Check if it works.

local connectionAdded = zone.playerAdded:Connect(function(player)
   
   safecheck = true
   if safecheck == true then
      -- checks if there's less than 5 players in the zone
      if playercount < 5 then
         playercount = playercount + 1
         print(playercount)
         print(player.Name)
      else
      -- teleports the player and returns the function if playercount => 5
         local char = player.Character
         if char and char:FindFirstChild("HumanoidRootPart") then
            char.HumanoidRootPart.CFrame = CFrame.new(TeleportPart.Position, char.HumanoidRootPart.Position)  
         end
         return
      end
   end
   
   local char = player.Character
   local forceField = char and char:FindFirstChild(forceFieldName)
   if not forceField then
      forceField = forceFieldTemplate:Clone()
      forceField.Parent = char
   end
end)
3 Likes

Remember to change TeleportPart to wherever you want to teleport them.

1 Like

Okay thanks so this will teleport only the sixth player to spawn because I just want five players there.

Yes. But try testing it first.

Okay so I wrote like for example (20, 20, 20) in place for Teleport Part? So Where I want the sixth player to teleport back to?

char.HumanoidRootPart.CFrame = CFrame.new(Vector3.new(20,20,20), char.HumanoidRootPart.Position) 

This will teleport the 6th player to 20,20,20

I am not sure but when I test it normally it would give u a force field before right? But now the force field is gone and when I join inside the zone, playercount variable also does not show the count. Is there something wrong with the if statement?

Are there any errors in output?

i will show you a video of my before product.

Hm no but it just doesnt show my playercount variable now

Okay my video is here.
1
InquistorWasBanned
Because I am in the zone and number 1 stands for Playercount Value and the name of the person which caused it. But now there isnt anything showing and the force field is gone

. THe video is in roblox

That is before I published on roblox studio

I think i know why for the teleport function

and for the script u said

which is a little different in the middle.

Can you show me the script again?

okay my old script is

local ZonePlus = require(4664437268)
local ZoneService = require(ZonePlus.ZoneService)


-- Setup zone
local group = workspace.SafeZone2
local zone = ZoneService:createZone("SafeZone2", group, 0)
local safecheck = false
local playercount = 0
local player = game.Players.LocalPlayer
local table = {}

   
   


-- Create a safe zone by listening for players entering and leaving the zone
local safeZoneCheckInterval = 0.2
local forceFieldName = "PineapplesForLife"
local forceFieldTemplate = Instance.new("ForceField")
forceFieldTemplate.Name = forceFieldName

local connectionAdded = zone.playerAdded:Connect(function(player)
   
   safecheck = true
   if safecheck == true then
   	playercount = playercount + 1
   	print(playercount)
   	print(player.Name)
   	
   
   	
   end
   
   local char = player.Character
   local forceField = char and char:FindFirstChild(forceFieldName)
   if not forceField then
   	forceField = forceFieldTemplate:Clone()
   	forceField.Parent = char
   	
   end
end)
local connectionRemoving = zone.playerRemoving:Connect(function(player)
   safecheck = false
   if safecheck == false then
   	playercount = playercount - 1
   	print(playercount)
   	print(player.Name)
   end
   
   local char = player.Character
   local forceField = char and char:FindFirstChild(forceFieldName)
   if forceField then
   	forceField:Destroy()
   
   	end
   
end)

zone:initLoop(safeZoneCheckInterval)