How To Make A Required Players Queue

How do you make it so that the script only runs when there is a required amount of players? Which is 3?
This is the script. This is not my script, this is PonchoKing’s script. I don’t know how to script :frowning:

local TweenService = game:GetService("TweenService")
local placeId = "10131371619"
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
1 Like

I’ve tried to solve this problem a couple of times, but I haven’t figured out how to do it.

local function teleportPlayers()
	if #list > 0 then

set to

local function teleportPlayers()
	if #list >= 3 then

It checks if the list has at least 3 players and then teleports them

if #list < 16 then

No clue what this line does though

I’m not sure if this helps, maybe changing some of the lines would be better, its just for efficiency.

table.insert(list,char.Name)

Instead of char.Name, just add the player, table.insert(list, player).
For the for loop you can just do:

for i, player : Player in list do -- you can just ignore the : Player, it is just to define the variable
	if game.Players:GetPlayerByUserId(player.UserId) then
		table.insert(playersToTeleport, player)
		TransitionEvent:FireClient(player)	
	else
		list[i] = nil
	end
end

In the removeFromList function:

local function removeFromList(Player : Player)
	for i, player : Player in list do
		if player == Player then
			table.remove(list,i)
			updateGui()
		end
	end
end
1 Like

Doing that just breaks it. (30 aaaaaaa)

It says teleporting, but it doesn’t actually teleport, even when I have 3 players. Here is the script:

local TweenService = game:GetService("TweenService")
local placeId = "10131371619"
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(Player : Player)
	for i, player : Player in list do
		if player == Player then
			table.remove(list,i)
			updateGui()
		end
	end
end

local function teleportPlayers()
	if #list >= 3 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, player : Player in list do -- you can just ignore the : Player, it is just to define the variable
			if game.Players:GetPlayerByUserId(player.UserId) then
				table.insert(playersToTeleport, player)
				TransitionEvent:FireClient(player)	
			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

It just counts down and loops, it doesn’t actually teleport the players.

Players wont be teleported if in the studio, if youre in the ROBLOX Player, continue reading.

Im not the best at scripting myself, but i would assume theres an issue in the part where it gets the players to teleport.
Im not entirely sure how youve written your script, but i have seen that youre saving the players usernames in the list table, so heres a script for adding them to the playersToTeleport table.

for i, v in pairs(list) do
	if game.Players:FindFirstChild(v) then
		table.insert(playersToTeleport, player)
		TransitionEvent:FireClient(player)
	else
		table.remove(list, i)
	end
end

Gonna add on here, i am an idiot. Dont expect this to work :slight_smile:

That’s because you are inserting the character’s name not the player, change it to player:

local Players = game:GetService("Players")

script.Parent.Gate.Touched:Connect(function(hit)
	if hit.Parent:findFirstChild("Humanoid") then
		if teleporting == false then
			local char = hit.Parent
			local localPlayer = Players:GetPlayerFromCharacter(char)

			if localPlayer then
				for _, player : Player in list do
					if player == localPlayer then
						return
					end
				end
				--If it did not returned/find the player in the list
				if #list < 16 then
					table.insert(list, localPlayer)
					char:SetPrimaryPartCFrame(spawnTeleport.CFrame)
					updateGui()
					leaveGuiEvent:FireClient(player)
				
					player.CharacterRemoving:connect(function(character)
						removeFromList(character)
					end)
				end
			end
		end
	end
end)

Thank you! It works, but after it teleports the players, you can’t teleport the players again with the same server.

This is the updated script:

local TS = game:GetService("TeleportService")
local TweenService = game:GetService("TweenService")
local placeId = "10131371619"
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(Player : Player)
	for i, player : Player in list do
		if player == Player then
			table.remove(list,i)
			updateGui()
		end
	end
end

local function teleportPlayers()
	if #list >= 3 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, player : Player in list do -- you can just ignore the : Player, it is just to define the variable
			if game.Players:GetPlayerByUserId(player.UserId) then
				table.insert(playersToTeleport, player)
				TransitionEvent:FireClient(player)	
			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

local Players = game:GetService("Players")

script.Parent.Gate.Touched:Connect(function(hit)
	if hit.Parent:findFirstChild("Humanoid") then
		if teleporting == false then
			local char = hit.Parent
			local localPlayer = Players:GetPlayerFromCharacter(char)

			if localPlayer then
				for _, player : Player in list do
					if player == localPlayer then
						return
					end
				end
				--If it did not returned/find the player in the list
				if #list < 100 then
					table.insert(list, localPlayer)
					char:SetPrimaryPartCFrame(spawnTeleport.CFrame)
					updateGui()
					leaveGuiEvent:FireClient(player)

					player.CharacterRemoving:connect(function(character)
						removeFromList(character)
					end)
				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

You can use

game.Players.PlayerAdded

and

game.Player.PlayerRemoving

to trigger functions that either add or remove a player Instance to/from a table. Then make another function that checks the list that gets called in either the PlayerAdded or PlayerRemoving event and runs appropriate code when enough players are present.

Example:

players = {}

started = 0

function CheckPlayers()
	if #players > 3 then
		started = 1
		-- insert code here
	elseif started == 1 and #players < 3 then
		-- restart everything if that's what you want
	end
end


game.Players.PlayerAdded:Connect(function(plr)
	table.insert(players,plr)
	print(players)
	CheckPlayers()
end)

game.Players.PlayerRemoving:Connect(function(plr)
	table.remove(players,table.find(players,plr))
	print(players)
	CheckPlayers()
end)

I don’t know how to script, I tried some things, but I just didn’t know how to do it. Can you explain it in even simpler terms?

Also, this script doesn’t work anymore, it made it so that there is a button which lets you leave the queue, here is the script:

local leaveGuiEvent = game.ReplicatedStorage.LeaveGuiEvent
local player = game.Players.LocalPlayer
player.CameraMaxZoomDistance = 50

leaveGuiEvent.OnClientEvent:Connect(function()
	player.PlayerGui.MainGui.leaveButton.Visible = true
end)

player.PlayerGui.MainGui.leaveButton.MouseButton1Down:connect(function()
	leaveGuiEvent:FireServer()
	player.PlayerGui.MainGui.leaveButton.Visible = false
end)

This what I tried to do:

local TS = game:GetService("TeleportService")
local TweenService = game:GetService("TweenService")
local placeId = "10131371619"
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(Player : Player)
	for i, player : Player in list do
		if player == Player then
			table.remove(list,i)
			updateGui()
			CheckPlayers()
		end
	end
end

local function teleportPlayers()
	if #list >= 1 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, player : Player in list do -- you can just ignore the : Player, it is just to define the variable
			if game.Players:GetPlayerByUserId(player.UserId) then
				table.insert(playersToTeleport, player)
				TransitionEvent:FireClient(player)	
			else
				table.remove(list, i)
			end
		end 
		local code = TS:ReserveServer(placeId)
		teleporting = true
		teleporting = false
		TS:TeleportToPrivateServer(placeId,code,playersToTeleport)
	end
end

function CheckPlayers()
	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
	updateGui()
end


local Players = game:GetService("Players")

script.Parent.Gate.Touched:Connect(function(hit)
	if hit.Parent:findFirstChild("Humanoid") then
		if teleporting == false then
			local char = hit.Parent
			local localPlayer = Players:GetPlayerFromCharacter(char)

			if localPlayer then
				for _, player : Player in list do
					if player == localPlayer then
						return
					end
				end
				--If it did not returned/find the player in the list
				if #list < 100 then
					table.insert(list, localPlayer)
					char:SetPrimaryPartCFrame(spawnTeleport.CFrame)
					updateGui()
					leaveGuiEvent:FireClient(player)

					player.CharacterRemoving:connect(function(character)
						removeFromList(character)
					end)
				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

Don’t understand why it isn’t working.