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