Region3 Lobby Teleport problem

  1. What do you want to achieve? Keep it simple and clear!
    Trying to create a region, upon entering which a player is added to the table, (and later teleports to another place.)

  2. What is the issue? Include screenshots / videos if possible!
    The problem is i don’t know how to remove a player from the table when he leaves the region.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    Made a check for the presence of a player in the table, and the limit of players. But, i don’t know what to do next.

local regionPart = script.Parent
local playersInRegion = {}
local limit = 10


coroutine.resume(coroutine.create(function()
	while wait(1) do
		print("Total Players: "..#playersInRegion)
	end
end))

while wait(1) do
	local region = Region3.new(regionPart.Position - (regionPart.Size/2), regionPart.Position + (regionPart.Size/2))
	local partsInRegion = game.Workspace:FindPartsInRegion3(region, regionPart, 200)

	for i, part in pairs(partsInRegion) do
		if part.Parent:FindFirstChild("Humanoid") ~= nil then

			local player = game.Players:GetPlayerFromCharacter(part.Parent)

			if #playersInRegion < limit then
				if table.find(playersInRegion, player) then
					warn(tostring(player).." is is already in table!")
				else
					table.insert(playersInRegion, player)
					print(tostring(player).." found in region, Added to table.")
				end
			else
				print("Max Players!")
			end
		end
	end	
end

local regionPart = script.Parent
local playersInRegion = {}
local limit = 10


coroutine.resume(coroutine.create(function()
	while task.wait(1) do
		print("Total Players: "..#playersInRegion)
	end
end))

while task.wait(1) do
	local region = Region3.new(regionPart.Position - (regionPart.Size/2), regionPart.Position + (regionPart.Size/2))
	local partsInRegion = workspace:FindPartsInRegion3(region, regionPart, 200)
	playersInRegion = {}
	for i, part in pairs(partsInRegion) do
		if part.Parent:FindFirstChild("Humanoid") then
			local player = game.Players:GetPlayerFromCharacter(part.Parent)
			if player then
				if table.find(playersInRegion, player) then
					warn(tostring(player).." is is already in table!")
				else
					table.insert(playersInRegion, player)
					print(tostring(player).." found in region, Added to table.")
					print("Max Players!")
				end
			end
		end
	end	
end

You can just reset the players in Region3 array every loop, also I’ve added a check to make sure “player” isn’t nil otherwise errors could occur.

1 Like

Brilliant! But I have two questions
Why do you do playersInRegion = {} instead of table.clear?
And why task.wait() instead of wait() ?

task.wait() is more reliable than wait() and assigning an empty table instead of calling table.clear() is entirely optional, feel free to use whichever.