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)
2 Likes
Seems like you’re checking this from a LocalScript? That’s a bad idea security-wise, and actually makes things harder for you.
Here’s how I’d do it:
local ZonePlus = require(4664437268)
local ZoneService = require(ZonePlus.ZoneService)
-- Setup zone
local group = workspace.SafeZone2
local zone = ZoneService:createZone("SafeZone2", group, 0)
local maxPlayers = 5
local numPlayers = 0
-- 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
zone.playerAdded:Connect(function(player)
numPlayers += 1
if numPlayers > maxPlayers then
--move player back to spawn
-- don't decrement numPlayers, that gets handled by the playerRemoving connection
end
local char = player.Character
local forceField = char and char:FindFirstChild(forceFieldName)
if not forceField then
forceField = forceFieldTemplate:Clone()
forceField.Parent = char
end
end)
zone.playerRemoving:Connect(function(player)
numPlayers -= 1
local char = player.Character
local forceField = char and char:FindFirstChild(forceFieldName)
if forceField then
forceField:Destroy()
end
end)
zone:initLoop(safeZoneCheckInterval)
It’s hard to know if it’s going to work though, because I don’t know what ZonePlus is. Where is the documentation for ZonePlus?
1 Like
Oh, I got it Thanks but how would I refer to the sixth player and teleport them? Would I use the Player:MovetoVector? I think it would have to be something like refering to the exact 6th player who joins and move them back to base. How would I do that> Also I think it is working so far! Maybe save the Player Ids? And when they leave the area it would take out them from the table? But how can I script that?
Thanks!
1 Like
Here’s a version that doesn’t just count the number of players, it keeps track of which players are there as well. It’s not necessary to do it this way, but here it is anyway:
local ZonePlus = require(4664437268)
local ZoneService = require(ZonePlus.ZoneService)
-- Setup zone
local group = workspace.SafeZone2
local zone = ZoneService:createZone("SafeZone2", group, 0)
local maxPlayers = 5
local playersInZone = {}
-- 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
--The players that are in the zone can only change when a player enters or leaves the zone.
--By updating the table to hold the correct players when either event occurs, we know that
-- the table always holds the correct players. Any time the table can become out-dated, we
-- update it, causing it to never be out of date.
zone.playerAdded:Connect(function(player)
--Insert a player into the table when they enter the zone. That way, when a player enters the zone, the correct players will be in the table.
table.insert(playersInZone, player)
if #playersInZone > maxPlayers then
--If there are too many players in the zone after this player has entered, that player is the (maxPlayers + 1)th player in the zone,
-- so they are the correct player to move out of the zone to decrease the number of players in the zone to the max.
--Move player back to spawn
-- don't remove player from the table, that gets handle by the playerRemoving connection.
end
local char = player.Character
local forceField = char and char:FindFirstChild(forceFieldName)
if not forceField then
forceField = forceFieldTemplate:Clone()
forceField.Parent = char
end
end)
zone.playerRemoving:Connect(function(player)
--Remove a player from the table when they leave the zone. That way, when a player leaves the zone, the correct players will be in the table.
table.remove( playersInZone, table.find(playersInZone, player) )
local char = player.Character
local forceField = char and char:FindFirstChild(forceFieldName)
if forceField then
forceField:Destroy()
end
end)
zone:initLoop(safeZoneCheckInterval)
Okay thanks which function would be able to teleport the player? So like how would I be able to write the teleport and find which to move back? I am a little stuck on that part because I think all I need to do is to be able to teleport but how would I do that? Thanks!
1 Like
What do you mean teleport? Because there’s a specific meaning to “teleporting” in Roblox, i.e. sending players to different Places or even different Games.
If you just mean moving players around in the game world, you can do that by setting the Position of the players’ characters. Something like
local function putPlayerAtPosition(player, position)
local character = player.Character
if character then
local rootPart = character.PrimaryPart
if rootPart then
rootPart.Position = position
end
end
end
okay thanks so i would put that in where you put the comments for teleporting? And I would substitute position for the position I want?
Thanks!