Before you say that this has been done 1000 times, this is a separate occasion. I have basic knowledge of coding and my queue system already “technically” works. The problem is, that it comes with a lot of bugs and is inefficient to the flow of the game. May someone provide clear education on how I can improve my code and potentially store it in ServerScriptStorage instead of the folder it is in? (This means that if I duplicate the elevator then it will work for each individually!)
local t_TPService = game:GetService("TeleportService")
local t_PlrService = game:GetService("Players")
local PlayersInQueue = {}
local sumofplayers = 0
local maxval = 4
game.Players.PlayerAdded:Connect(function(plr)
end)
game:IsLoaded(task.wait(5.5))
local evsys = workspace:FindFirstChild("Elevator1-4")
local isinteleport = false
local maxplayers = evsys.MaxPlayers
local function elevatorEntered()
evsys.TouchElevators.TouchElevator1.Touched:Connect(function(hit)
local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
if Player then
if sumofplayers == maxval then
print("Full")
else
if isinteleport == true then
else
Player.Character.PrimaryPart.CFrame = evsys.ElevatorTeleportParts.TeleportPart.CFrame
isinteleport = true
game.ReplicatedStorage.Bricks.InQueue:FireClient(Player)
table.insert(PlayersInQueue, Player.Name)
sumofplayers = sumofplayers+1
print(PlayersInQueue)
if #PlayersInQueue == 0 then
print("Empty Table")
else
if sumofplayers>=1 then
task.wait(15)
local TweenService = game:GetService("TweenService")
local info1 = TweenInfo.new(0.8, Enum.EasingStyle.Bounce, Enum.EasingDirection.Out)
local goal1 = {CFrame = script.Parent.MoveTo1.CFrame}
local Tween1 = TweenService:Create(script.Parent.ElevatorDoor1.PrimaryPart, info1, goal1)
local info2 = TweenInfo.new(0.8, Enum.EasingStyle.Bounce, Enum.EasingDirection.Out)
local goal2 = {CFrame = script.Parent.MoveTo2.CFrame}
local Tween2 = TweenService:Create(script.Parent.ElevatorDoor2.PrimaryPart, info2, goal2)
Tween1:Play()
Tween2:Play()
task.wait(3)
local playersstillin= game.Players:GetChildren(PlayersInQueue)
for i=1, #playersstillin do
local playerin = playersstillin[i]
local theplayer = game.Players:FindFirstChild(playerin)
game.ReplicatedStorage.Bricks.Teleport:FireClient(playerin)
local getPlayer = game.Players:GetChildren(PlayersInQueue)
local accessCode = t_TPService:ReserveServer(16767757091)
t_TPService:TeleportToPrivateServer(16767757091, accessCode, getPlayer, nil, nil, script.LoadingScreen)
end
end
end
end
end
end
end)
end
elevatorEntered()
game.ReplicatedStorage.Bricks.Exit.OnServerEvent:Connect(function(player)
local char = workspace:FindFirstChild(player.Name)
char.PrimaryPart.CFrame = workspace["Elevator1-4"].CameraPart.CFrame
isinteleport = false
local playertable = table.find(PlayersInQueue, player.Name)
table.remove(PlayersInQueue, playertable)
print(PlayersInQueue)
sumofplayers = sumofplayers-1
end)
Initializing “PlayersInQueue = {}” at the begining of the script won’t cause errors for one elevator but if you add another elevator errors and bug will occur.
Instead you can create a for loop for each elevator and individually script their behaviour.
Instead of counting the number of players with “sumofplayers = 0” you can check their count with #PlayersInQue which will return the same.
There is no function that removes the player from the elevator if he leaves. If the player is inside the elevator and leaves, the script won’t change the sumofplayers or the PlayersInQueue value.
local t_TPService = game:GetService("TeleportService")
local t_PlrService = game:GetService("Players")
local PlayersInQueue = {}
--local sumofplayers = 0 -- There is no need to count the players becouse we can use #PlayersInQueue to get the count of players in the table
local fld_Teleporters = game.Workspace.Teleporters -- You can store all teleporters in a folder in workspace (the part that player touches to enter)
local func_TeleportPlayers = function(tbl_PlayersInElevator)
for _, plr in pairs(tbl_PlayersInElevator) do
print("Teleporting: "..plr.Name)
local accessCode = t_TPService:ReserveServer(16767757091)
t_TPService:TeleportToPrivateServer(16767757091, accessCode, plr, nil, nil, script.LoadingScreen)
end
end
-- for each part (elevator) in the folder that the teleporters are stored..
for _, part_elevator :Part in pairs(fld_Teleporters:GetChildren()) do
local MaxPlayers = part_elevator.MaxPlayers.Value -- The int value inside each elevator that stores the max value of players for teleport
local tbl_PlayersInElevator = {} -- the table stores the players inside the elevator
part_elevator.Touched:Connect(function(hit)
if(#tbl_PlayersInElevator == MaxPlayers) then return end -- Will return end if the max players count is reached
local Player = t_PlrService:GetPlayerFromCharacter(hit.Parent)
if(Player == nil) then return end -- If the hit is not player, will return
if(table.find(tbl_PlayersInElevator, Player)) then return end -- Checking if player is alreay in the elevator
tbl_PlayersInElevator[#tbl_PlayersInElevator+1] = Player -- adding the player inside the table
if(#tbl_PlayersInElevator == MaxPlayers) then
func_TeleportPlayers(tbl_PlayersInElevator)
end
end)
part_elevator.TouchEnded:Connect(function(hit)
local Player = t_PlrService:GetPlayerFromCharacter(hit.Parent)
for i, plr in pairs(tbl_PlayersInElevator) do
if(plr.Name == Player.Name) then -- if the player is still inside the elevator..
table.remove(tbl_PlayersInElevator, i) -- remove from the table
end
end
end)
t_PlrService.PlayerRemoving:Connect(function(plr)
for i, plrInElevator in pairs(tbl_PlayersInElevator) do
if(plr.Name == plrInElevator.Name) then -- if the leaving player is inside the elevator...
table.remove(tbl_PlayersInElevator, i) -- remove from the table
end
end
end)
end
This is my example for teleport elevator script. I used for loop to get through each elevator and the script must be placed inside ServerScriptService in order to run.
I haven’t add the tween animations and etc.
The elevators are located in game.Workspace.Teleporters:GetChildren()
The elevators are parts with no collision.
If you have questions, something not working or I have a mistake please reply
Hey, just one question: What is stored as “part_elevator?”
EDIT:
for i, plrInElevator in pairs(tbl_PlayersInElevator) do
if(plr.Name == plrInElevator.Name) then -- if the leaving player is inside the elevator...
table.remove(tbl_PlayersInElevator, i) -- remove from the table
end
end
The part_Elevator is the elevator. Thats the part players touch and get teleported if it reaches the max player count.
Thats one elevator. The for loop goes through each elevator located in game.Workspace.Teleporters
Of course you can change the location of the teleporters
for _, part_elevator :Part in pairs(fld_Teleporters:GetChildren()) do
for every part (elevator) inside the folder “Teleporters” located in workspace do…
for i, plrInElevator in pairs(tbl_PlayersInElevator) do
if(plr.Name == plrInElevator.Name) then -- if the leaving player is inside the elevator...
table.remove(tbl_PlayersInElevator, i) -- remove from the table
end
end
This loop if to locate the player inside the table that is leaving, so there won’t be any other unexpected bugs.
For every player that is in the elevator (also located in table tbl_PlayersInElevator) do
Checks if their names are the same.
And removes the player from the table.
Please reply the errors that occur and ask if you don’t understand something.
The issue is becouse there is no check whether the Player is nil.
When touch occurs, the “hit” can be anything - from just a single part to player character.
The issue will be fixed when you add: