How would I go about this?

Alright so I kind of need help with this script, basically I need to know how to make a teleporting que, like Camping, Hotel, etc… but I have no idea what to do next. Here’s the code I have now.

local part = script.Parent

local TS = game:GetService("TeleportService")

local TimeLabel = part.SurfaceGui.Time

local Folder = part.Players

local PlayersIn = Folder.Player1

local StartTime = 30


while wait(1) do
	StartTime -=1
	TimeLabel.Text = "Teleports in: "..StartTime
	if StartTime == 0 then
		StartTime = 31
	end
end



part.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
		
		PlayersIn.Value = Player.Name
		
		local ReservedPrivateServer = TS:ReserveServer(10032135443)
		
		wait(StartTime)
		
		TS:TeleportToPrivateServer(10032135443, ReservedPrivateServer, game.Players:FindFirstChild(PlayersIn.Value))
		
	end
end)

So, if you could help me, that would be gladly appreciated!

Thanks!

NOTE: Posted this twice now, really would help!

3 Likes

What is the issue with this code?

There is none, I’m just wondering what I should do next

Well, what do you want to do next?

A teleporting que kind of thing

Isn’t that what it does? How many people can teleport at a time?

So my plan for this one is only one, but I want to have multiple, and I want it so they can exit, if you could explain how that could be done, it’d be very much appreciated. (not in code, just a quick explanation)

Could I connect the UI so I could exit the que?

Hi there, I’m the guy that responded to your post that started this.
I found a tutorial on YouTube for making a story game, maybe you can find your answer from there and continue on your own!

Did you make this up yourself? Its not really a queue is it? Its one object that if you touch it, it counts the time down then teleports the one player.

If this script is duplicated for other objects how will they coordinate between scripts to teleport multiple players to the same server?

As far as exiting the ‘queue’, if you were using a seat that would be an easy way to signal a want to exit. Otherwise you’d need another trigger part or a gui button or something.

1 Like

I Believe that your approach for this is not entirely correct. Your code right now is pretty much written to only run on a single player at once as .Touched only returns a single part on every connection while each connection being entirely separate from any before it. This also causes the StartTime wait of the function to simply just run independently of other players.
Personally i would create a empty table somewhere at the top of the script something like this:

local PlayersInQueue = {}

And then i would instead use the Touched function in this fashion:

QueuePart.Touched:Connect(function(TouchedPart)
	if TouchedPart.Parent:FindFirstChild("Humanoid") then
		local Player = game.Players:GetPlayerFromCharacter(TouchedPart.Parent)
		
		local AlreadyExists = table.find(PlayersInQueue, Player)
		if not AlreadyExists then
			table.insert(PlayersInQueue, #PlayersInQueue+1, Player)
		end
	end
end)

Basically, you would add the player that touched said part to the table. Allowing multiple players to touch it and have all of them get added to exactly the same queue.
Now to actually get said queue to function, you would need to create a new CurrentTime variable along with a new Heartbeat function of RunService in order to create an actual functional timer.

RunService.Heartbeat:Connect(function(DeltaTime : number)
	CurrentTime -= DeltaTime
	
	TimeLabel.Text = "Teleports in: "..math.floor(CurrentTime)
	
	if CurrentTime <= 0 then
		CurrentTime = 30
		
		TeleportService:TeleportToPrivateServer(10032135443, ReservedPrivateServer, {PlayersInQueue})
		PlayersInQueue = {}
	end
end)

As you can see, the rest is all handled by a single function that will be able to not only update the text of how many seconds are remaining, but should also teleport all of the players in said queue to your desired place. Also the “math.floor” is being used to round the CurrentTime value as normally the number itself will have preeeetty long decimals. The code itself will also reset the timer to 30 while clearing the Player queue once it has finished counting down.

Slight edit: Noticed a small oversight in the Touched function code where the same player could be stacked a million times in the queue, my bad, should be fixed now.

2 Likes

Good points. I’m not sure I’d use the heartbeat timer, seems a bit overkill for this. I’d make it where the timer doesn’t start till someone ‘joins’ the queue. Then, if everyone leaves, the timer resets and sits fixed at 30 till someone ‘joins’.

Also, if you would wish to also have the ability to remove players from the queue when they walked away then what you would need to do is loop through the PlayersInQueue list, and do a spatial query function using Workspace:GetPartsInPart() with the OverlapParams set to whitelist only the player character. Usually it will return a table and if that table is 0 or less, then it means the player is no longer in the queue and needs to be removed from the list. Example:

for Key, Player in pairs(PlayersInQueue) do
		local PlayerCharacter = Player.Character
		local PlayerRootPart = PlayerCharacter.HumanoidRootPart -- Kinda expecting the humanoid root part to exist at this point
		
		local QueueOverlap = OverlapParams.new()
		QueueOverlap.FilterType = Enum.RaycastFilterType.Whitelist
		QueueOverlap.FilterDescendantsInstances = {PlayerCharacter}
		
		local FoundInQueue = workspace:GetPartsInPart(QueuePart, QueueOverlap)
		if #FoundInQueue <= 0 then
			table.remove(PlayersInQueue, Key)
		end
	end

This piece of code should also be easily copy-pasteable right into the Heartbeat. Hope this helps!

The heartbeat itself is just there just for potentially better practice along with potentially cleaner code since usually using a while loop, it may or may not delay any code under it from activating, sometimes maybe even just stopping it from running entirely. The function itself can 100% be adjusted to only run whenever there are actually any players in it too.

1 Like

I am trying to make ‘Queue’ part, thank you for correcting my spelling, but yes, I am trying to create a queue part, thing is I’m not using seats, I’m just letting them touch the part, and they would be teleported to the game, and if they want to leave I would have to make UI for them to leave.

Thank you, this explained a lot for me!

1 Like

Okay, you could also make a zone with a nocollide part and add/remove them if they’re inside or outside of it. To do this use WorldRoot:GetPartsInPart () to track the players.

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