You can write your topic however you want, but you need to answer these questions:
What do you want to achieve? Keep it simple and clear!
I want to make an elevator similar to tower defense simulator’s elevator
What is the issue? Include screenshots / videos if possible!
The text showing the amount of time before they teleport and the number of players in the elevator is not working
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I forgot the solutions I tried, No.
The code:
local teleportService = game:GetService("TeleportService")
local elevator = workspace.Lobby.Elevator
local playerss = elevator.players
local timee = elevator.time
local queue = {}
local touchPart = elevator.touchpart
local maxPlayers = 4
function teleportPlayers(playerArray)
local teleportOptions = Instance.new("TeleportOptions")
teleportOptions:SetTeleportData(teleportData)
teleportService:TeleportAsync(placeId, playerArray, teleportOptions)
end
function setUp()
playerss.SurfaceGui.SIGN.Text = "0/" .. maxPlayers .. " Players"
timee.SurfaceGui.SIGN.Text = "Players Needed!"
queue = nil
queue = {}
end
setUp()
function returnTable()
return queue
end
function countDown()
for i = 15, 0, -1 do
task.wait(1)
playerss.SurfaceGui.SIGN.Text = #queue .. " /" .. maxPlayers .. " Players"
timee.SurfaceGui.SIGN.Text = i .. " seconds left!"
if i == 0 then
teleportPlayers(queue)
setUp()
break
elseif #queue == 0 then
setUp()
break
end
end
end
game.ReplicatedStorage.queue.OnInvoke = returnTable
touchPart.Touched:Connect(function(hit)
if hit.Parent:IsA("Model") and game.Players:GetPlayerFromCharacter(hit.Parent) then
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
local character = player.Character
teleportData = {
placeId = placeId;
jobId = teleportService:ReserveServer(placeId);
money = game.ServerStorage:WaitForChild(player.UserId):WaitForChild("Money").Value;
}
if #queue >= 1 then
coroutine.wrap(countDown)
if #queue == 4 then
touchPart.CanTouch = false
elseif #queue > 4 then
for _, player in ipairs(queue) do
game.Players:FindFirstChild(player):Kick("Suspicious activity detected while in elevator! Rejoin.")
end
end
end
if table.find(queue, player) then
print("Player is already in queue!")
else
table.insert(queue, player)
character.PrimaryPart.CFrame = elevator.tpPart.CFrame
game.ReplicatedStorage.Camera:FireClient(player)
end
game.Players.PlayerRemoving:Connect(function(player)
if table.find(queue, player) then
table.remove(queue, table.find(queue, player))
end
end)
end
end)``
Instead of using Part.Touched as a detector for a player entering and leaving, I’d recommend using Part:GetTouchingParts() and connecting it with run service to see how many players are currently in the queue.
local plrs = {}
local part = workspace.queueBox -- arbitrary box for the queue
game:GetService("RunService").Stepped:Connect(function()
local parts = part:GetTouchingParts()
for _,x in pairs(parts) do
if x.Parent:FindFirstChild("Humanoid") then
local plr = game.Players:GetPlayerFromCharacter(x.Parent)
plrs[plr.UserId] = plr -- allows for no duplicates
end
end
end)
Let’s do this experimentally. You will require more than two people though.
Create a regular Part in workspace, and make a script. Put this inside:
This is only a template, you’ll have to modify some key things to suit your elevator.
local players = {}
local amountNeeded = 2
function checkPlayers(amount)
if amount >= amountNeeded then
print("ready")
end
end
script.Parent.Touched:Connect(function(otherPart)
if otherPart.Parent:FindFirstChildOfClass("Humanoid") then
for _,player in pairs(players) do
if player == otherPart.Parent.Name then return end
end
table.insert(players,otherPart.Parent.Name)
checkPlayers(#players)
end
end)
script.Parent.TouchEnded:Connect(function(otherPart)
if otherPart.Parent:FindFirstChildOfClass("Humanoid") then
for index,player in pairs(players) do
if player ~= otherPart.Parent.Name then return end
end
table.remove(players,index)
checkPlayers(#players)
end
end)