Help with Que system for story game

I want my que system to detect when a player enters, save that player to a table and then send the players into the table to the next place(my other baseplate inside of the game using teleport service)

The problem is i messed up big time and have no idea if i can fix it or i have to rewrite everything.

  1. problem is that when any player enters since its on the server side it sets a boolean value inside the script called playerQued to true and instead of only doing this for the player that entered it sets it for every player and makes it so no one can join the room until the player that entered has exited.Althought this would work for a single player thats not what i intended. I have intended this to be a 0/4 lobby type thing. there is multiple other problems such as when the player enters and exits then re enters sometimes the timer will start two countdowns at the sametime and continue the last countdown while also starting a new one. I didnt even realize the hole i was walking myself into writing the code until after i thought i finished mostly. I also have not added a check for if the quedPlayers is <= 4 and quedPlayers is > 0 so dont worry about mentioning that. I am just wondering if this is at all saveable i been working on it for a while if not i understand.

3.I have no other solutions besides wondering if this script will ever be able to function or i have to redo it all.

local playersInQue = require(game.ReplicatedStorage.QuedPlayers)
local TeleportService = game:GetService("TeleportService")

local TARGET_PLACE_ID = 14267907640

local playerQued = false





for i, door in pairs(game.Workspace.Rooms:GetChildren()) do
	if door:IsA("Part") and door.Name == "Room" then
		door.Touched:Connect(function(hit)
			local character = hit.Parent
			local player = game.Players:GetPlayerFromCharacter(character)
			
			
			if player and not playerQued then
				
				playerQued = true
				table.insert(playersInQue, player.Name)
				--print(player, " was added to table")
				
				for i, name in pairs(playersInQue) do
					if name == player.Name then


						local roomNum = door:FindFirstChild("roomNumber").Value
						local hrp = character:FindFirstChild("HumanoidRootPart")

						if roomNum == door.roomNumber.Value then
								

								
							local destination = door.TeleportPos
							if destination:IsA("Attachment") and destination.Name == "TeleportPos" and destination.Parent.roomNumber.Value == roomNum then
								hrp.CFrame = destination.WorldCFrame + Vector3.new(0, 3, 0)

								local newExitGui = game.ReplicatedStorage:WaitForChild("ExitGui"):Clone()
								newExitGui.Parent = player.PlayerGui

								if #playersInQue > 0 then
									
									local exitQueEvent = game.ReplicatedStorage.ExitQue
									local billboardGui = destination.Parent.TimeUntilStart.BillboardGui
									local timeCount = billboardGui.timeCount
									
									local playerCountDisplay = door.playerCount.SurfaceGui.playerCount
									
									local function updatePlayersInQueDisplay()
										local playerCount = #playersInQue

										--print(playerCount, " -This is the current player count")
										playerCountDisplay.Text = tostring(playerCount.."/4")
									end
									
									updatePlayersInQueDisplay()
									
									local function resetCountdown()
										
									end
									
									exitQueEvent.OnServerEvent:Connect(function(player)
										if player then
											local playerNameToRemove = player
											local exitPos = door.ExitPos.WorldCFrame
											hrp.CFrame = exitPos + Vector3.new(0,3,0)
											for i = 1, #playersInQue do
												if player then
													table.remove(playersInQue, i)
													
													updatePlayersInQueDisplay()
													playerQued = false
													
													newExitGui:Destroy()
												end
											end
										end
									end)
									
									local countingDown = false
									for i = 5, 0, -1 do
										countingDown = true
										timeCount.Text = tostring(i)  -- Update the GUI with the new countdown value
										
										task.wait(1)
										if not playerQued and countingDown then
											timeCount.Text = tostring("Waiting for players")
											break
										end
									end
									if timeCount.Text == "0" then
										print("3")
										for i, playerName in pairs(playersInQue) do
											
											local player = game.Players:FindFirstChild(playerName)
											
											
											
											

											
											print(player, "Is now being transported to game")
											TeleportService:TeleportAsync(TARGET_PLACE_ID, {player})
										end
									end
									end	
						end
						task.wait(3)
							
							
						
						end
					end
				end
			end
		end)
	end
end
1 Like