Problems With TeleportService Matchmaking

Hello whoever is reading this, I wont waste alot of your time so lets get straight to the point.

I want to create a match-making service that is cross-server and uses TeleportService which teleports you when you stand on a part.

The issue is that it teleports every person in the server even if they’re not standing on the part and another issue is that its not cross-server.

I have searched all of the internet (well I think so) and haven’t found a solution.

This is my code:

local Part = script.Parent

local teleportService = game:GetService("TeleportService")

local teleportID = 8371409082
local teleporting = false

local Players = game:GetService("Players")

local debounce = true


local function teleportPlayers()
	local playersToTeleport = Players:GetPlayers()
	local teleportData = {
		PlaceID = game.PlaceId,
		JobID = game.JobId
	}
	local code = teleportService:ReserveServer(teleportID)
	teleporting = true
	teleportService:TeleportToPrivateServer(teleportID, code, playersToTeleport, teleportData)
end

Part.Touched:Connect(function(hit)
	local character = hit.Parent
	local player = game.Players:GetPlayerFromCharacter(character)
	if hit.Parent:FindFirstChild("Humanoid") and debounce == true then
		debounce = false
		teleportPlayers()
	end
end)
2 Likes

You’re on the right track checking if hit is a sibling to a humanoid, but then you’re calling teleportPlayers() with no parameters and it’s just grabbing everyone and teleporting them. I’d create a table and add players that touch it to said table, then once it meets the conditions you want (# people, time elapsed etc) pass the whole table to teleportPlayers(). You’d also want to check that those people are still touching the part at that point in-case someone has touched it and left.

To make this cross-server you’ll need a much more complex solution involving MessagingService to establish cross-server communication, wherein you’d pass a global table of players attempting to queue and synchronize sending them all to the same server.

2 Likes

Wouldn’t expect a reply this fast, let alone this detailed! I’ll make you solution since you solved all of my issues and gave me feedback in such detail! If its okay I will just ask how to put the player into the table if they step on it and how to remove them. Thanks!

So I worked out how to insert players into a table using this article: Adding players to a table - #2 by korky5000

But how do you make selected conditions like you said in your 1st post?

If you wanted to know if enough players were in the table, you could use #table to get how many items it contains and compare it to whatever number you want. If you wanted to do a count-down you could check when you add someone if they’re the first, and if so, create a timer.

Sorry to bother you again but what’s #table and how do you use it?

#table fetches the number of elements are stored within a table

local players={}

print(#players)

can also be used for like:

#game.Players:GetPlayers() -- (which is a table of all players)

How you would want to approach it is by table.insert() the player’s name and table.remove() by finding the player’s iteration and remove them

--sample:
local PlayerTable={}

--onhit
table.insert(PlayerTable, player.Name)

--if player leaves
for i=1, #PlayerTable, 1 do

if PlayerTable[i]==player.Name then

   table.remove(PlayerTable, i)
   end
end

--then after teleporting the entire table, reset it

PlayerTable={}
1 Like

Thanks for explaining it to me, I was really confused about the #table thing and you explained it clearly and simple. Thanks!

So I tried doing things like this:

if #playersToTeleport == 2 then
    teleport thing
end)

But it didn’t work, Sorry to disturb you i’m really bad at scripting.