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!
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
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)
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
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?
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)
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?
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
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)