Text showing time and number of players in queue not working

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to make an elevator similar to tower defense simulator’s elevator
  2. 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
  3. 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)``
8 Likes

Is this in a server or local script?

3 Likes

It’s in a serverscript. [30 words or whatever]

3 Likes
playerss.SurfaceGui.SIGN.Text = "0/" ..  maxPlayers .. " Players"

First off here, you’re trying to concatenate a number into the string. Try this instead:

playerss.SurfaceGui.SIGN.Text = "0/"..tostring(maxPlayers).." Players"

Another thing, you’re not even tracking when players enter or leave. You need to do that.

3 Likes
  1. You can concatenate numbers into strings.
  2. Didn’t I do that?
touchPart.Touched:Connect(function(hit)

and

game.Players.PlayerRemoving:Connect(function(player)
	if table.find(queue, player) then
		table.remove(queue, table.find(queue, player))
	end
3 Likes

Sorry, I forgot to scroll down on your script.

Let me look.

1 Like

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

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

I forgot to mention I’m using a button so the player can leave

2 Likes

I found out why, I was supposed to call the coroutine but I didn’t so it didn’t do anything

3 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.