Only 4 Player Teleporter

I wanna make a game with an teleporter and only 4 people can go in or like 10 people but idk how to do that i got the teleporter already done but idk how to set the limit of it of how many players can go in it.

I did search this problem already on youtube and roblox studio website
but i can’t find it here’s a picture of my teleporter

local TS = game:GetService("TeleportService")
local TweenService = game:GetService("TweenService")
local placeId = "6072224242"
local leaveGuiEvent = game.ReplicatedStorage.LeaveGuiEvent
local TransitionEvent = game.ReplicatedStorage.TransitionEvent
local list = {}
local gui = script.Parent.GuiPart.SurfaceGui
local billboard = script.Parent.billboardPart.billboardGui
local timer
local teleporting = false
local spawnTeleport = script.Parent.spawn

local function updateGui()
	gui.Frame.players.Text = #list
	billboard.Frame.players.Text = #list
end

local function removeFromList(character)
	for i=1,#list do
		if list[i] == character.Name then
			table.remove(list,i)
			updateGui()
		end
	end
end

local function teleportPlayers()
	if #list > 0 then
		script.Parent.GuiPart.SurfaceGui.Frame.Status.Text = "TELEPORTING"
		script.Parent.GuiPart.SurfaceGui.Frame.Status.TextColor3 = Color3.new(1,0,0)
		local playersToTeleport = {}
		local teleportTime = 0
		for i=1,#list do
			if game.Players:findFirstChild(list[i]) then
				table.insert(playersToTeleport,game.Players:findFirstChild(list[i]))
				TransitionEvent:FireClient(game.Players:findFirstChild(list[i]))
			else
				table.remove(list,i)	
			end
		end 
		local code = TS:ReserveServer(placeId)
		teleporting = true
		TS:TeleportToPrivateServer(placeId,code,playersToTeleport)
		repeat wait() until #list <= 0
		script.Parent.GuiPart.SurfaceGui.Frame.Status.Text = "READY"
		script.Parent.GuiPart.SurfaceGui.Frame.Status.TextColor3 = Color3.new(34/255,255/255,20/255)
		teleporting = false
	end
end

script.Parent.Gate.Touched:Connect(function(hit)
	if hit.Parent:findFirstChild("Humanoid") then
		if teleporting == false then
			local char = hit.Parent
			local player = game.Players:FindFirstChild(char.Name)
			local alreadyExists = false
			
			for i=1,#list do
				if list[i] == char.Name then
					alreadyExists = true
				end
			end
			
			if alreadyExists == false then
				if #list < 16 then
					table.insert(list,char.Name)
					char.PrimaryPart.CFrame = spawnTeleport.CFrame
					updateGui()
					leaveGuiEvent:FireClient(player)
				end
				
				player.CharacterRemoving:connect(function(character)
					removeFromList(character)
				end)
			end
			
		end
	end
end)

leaveGuiEvent.OnServerEvent:Connect(function(player)
	if player.Character then
		player.Character.HumanoidRootPart.Anchored = false
		wait()
		player.Character.Humanoid.Jump = true
		wait()
		player.Character:MoveTo(game.Workspace.leaveRoomPart.Position)
		removeFromList(player.Character)
	end
end)

while wait() do
	timer = 30
	for i=1,timer do
		timer = timer - 1
		gui.Frame.time.Text = timer
		billboard.Frame.time.Text = timer
		wait(1)
	end
	teleportPlayers()
end

I hope someone can help me please thx! if you wanna se it or test just download it.
TeleportPlace.rbxl (33.5 KB)

5 Likes

I would recommend you, on the place you teleport to, in the Game Settings set the Max Player setting to 4.

Also, I recommend you making a Script that checks if the Story, on the Server where you teleport to, is currently ongoing. If yes- re-teleport that player to another Story Server.

There is also another way to do what I said from here: Also, (you will need to do it yourself) to here: reteleport that player. The second way is also better, because it keeps players from teleporting infinietely. The method is: when the story starts on the Server, simply lock the Server. It will prevent players from joining Servers with round in progress. Don’t ask me to make a Script that locks the Story Server, because I wasn’t really ever interested in locking the Server using Roblox built-in Solutions.

1 Like

So i need to make a script what is checking how many players that are in the teleporter and when a player leaves he needs to get out of the list.

Couse that’s the problem couse idk how to do that.

I would recommend you just deleting the Player Count in the Teleporter.

I assume that you have created a Teleporter to an in-game Place. If yes, then:

  1. Go to your Game on the Roblox website.
  2. Click on the Configure This Game button.
  3. Go to Places.
  4. Find your Story Place.
  5. Click on that Place.
  6. Now, click Configure This Place.
  7. Go to Access and select Maximum Player Count to 4.
  8. All done!

As I said, you should lock the Story Server when it gets full. It’s good to do that to prevent people from joining that Server and playing (in some cases) from the middle of the Story.

I already did know how to do that but i mean how can you get only 4 players in the teleporter not in the game.

You should provide more information so I can clearly understand what do you mean.

So what i want to make is a teleporter where only 4 players can go in and if someone leaves there can be an extra play go in it i mean like the teleporters in tower defense simulator but i don’t really need it to be an elevator.

Use:

if #list >= 4 then
return warn("Max players!")
end
1 Like

I don’t really like giving the Scripts. Use some Values and Waits to achieve your success.

How do i get players out of the list if they leave? cause i know how to add a player but not get out of it.

Cause what you are trying to say is what i want.

You can use table.remove() to remove a player from a table.

Use one of this examples.

--Beggining of the Script, after a Table Local.    
game:GetService("Players").PlayerRemoving:Connect(function(player)
    --Use table remove here or add a little (like 0.8 wait) and assign a Table to local again.
    end)

Ok thx i think i found what i needed!

Here I used this before just change the variable I made at the top to the amount of players you want to go inside.

local AmountofPlayers = 4
local TS = game:GetService("TeleportService")
local TweenService = game:GetService("TweenService")
local placeId = "6072224242"
local leaveGuiEvent = game.ReplicatedStorage.LeaveGuiEvent
local TransitionEvent = game.ReplicatedStorage.TransitionEvent
local list = {}
local gui = script.Parent.GuiPart.SurfaceGui
local billboard = script.Parent.billboardPart.billboardGui
local timer
local teleporting = false
local spawnTeleport = script.Parent.spawn

    local function updateGui()
    	gui.Frame.players.Text = #list
    	billboard.Frame.players.Text = #list
    end

    local function removeFromList(character)
    	for i=1,#list do
    		if list[i] == character.Name then
    			table.remove(list,i)
    			updateGui()
    		end
    	end
    end

    local function teleportPlayers()
    	if #list > 0 then
    		script.Parent.GuiPart.SurfaceGui.Frame.Status.Text = "TELEPORTING"
    		script.Parent.GuiPart.SurfaceGui.Frame.Status.TextColor3 = Color3.new(1,0,0)
    		local playersToTeleport = {}
    		local teleportTime = 0
    		for i=1,#list do
    			if game.Players:findFirstChild(list[i]) then
    				table.insert(playersToTeleport,game.Players:findFirstChild(list[i]))
    				TransitionEvent:FireClient(game.Players:findFirstChild(list[i]))
    			else
    				table.remove(list,i)	
    			end
    		end 
    		local code = TS:ReserveServer(placeId)
    		teleporting = true
    		TS:TeleportToPrivateServer(placeId,code,playersToTeleport)
    		repeat wait() until #list <= 0
    		script.Parent.GuiPart.SurfaceGui.Frame.Status.Text = "READY"
    		script.Parent.GuiPart.SurfaceGui.Frame.Status.TextColor3 = Color3.new(34/255,255/255,20/255)
    		teleporting = false
    	end
    end

    script.Parent.Gate.Touched:Connect(function(hit)
    	if hit.Parent:findFirstChild("Humanoid") then
    		if teleporting == false then
    			local char = hit.Parent
    			local player = game.Players:FindFirstChild(char.Name)
    			local alreadyExists = false
    			
    			for i=1,#list do
    				if list[i] == char.Name then
    					alreadyExists = true
    				end
    			end
    			
    			if alreadyExists == false then
    				if #list < AmountofPlayers then
    					table.insert(list,char.Name)
    					char.PrimaryPart.CFrame = spawnTeleport.CFrame
    					updateGui()
    					leaveGuiEvent:FireClient(player)
    				end
    				
    				player.CharacterRemoving:connect(function(character)
    					removeFromList(character)
    				end)
    			end
    			
    		end
    	end
    end)

    leaveGuiEvent.OnServerEvent:Connect(function(player)
    	if player.Character then
    		player.Character.HumanoidRootPart.Anchored = false
    		wait()
    		player.Character.Humanoid.Jump = true
    		wait()
    		player.Character:MoveTo(game.Workspace.leaveRoomPart.Position)
    		removeFromList(player.Character)
    	end
    end)

    while wait() do
    	timer = 30
    	for i=1,timer do
    		timer = timer - 1
    		gui.Frame.time.Text = timer
    		billboard.Frame.time.Text = timer
    		wait(1)
    	end
    	teleportPlayers()
    end
3 Likes

Omg ty you did a great job :+1: