Adding players to a table

So I’m creating this teleport system where I need to teleport a specific players to a new server using TeleportToPrivateServer, I have that set up but I need help with scripting this:
So what I am trying to achieve is adding players to a table, via touch connect block then using the table to teleport players to the private server, how would I go about creating the table and adding players to the table?

10 Likes

A bunch of ways you can go about this, but I would do the following.

First, create an empty table of players you want to teleport:

local PlayersToTeleport = {}

When a player touches the specific part, add them to the table.

TouchBrick.Touched:Connect(function(TouchedPart)
	if TouchedPart.Parent:FindFirstChild("Humanoid") then
		local Character = TouchedPart.Parent
		local Player = game.Players:GetPlayerFromCharacter(Character)
		local AlreadyInTable = false
		-- Now make sure the player isn't already in the array
		for _,OtherPlayer in next,PlayersToTeleport do
			if OtherPlayer == Player then
				AlreadyInTable = true
			end
		end
		if not AlreadyInTable then
		-- Add them to the array!
		table.insert(PlayersToTeleport,Player)
		end
	end
end)

Now just simply use TeleportToPrivateServer using that array as the proper argument.

Let me know if you have any questions.

22 Likes

Thank you so much for this, I will try it out! Another question, how would I remove a player from a table say that they didn’t want to teleport with the group anymore? I’m doing this through an onserverevent function.

Also correct me if I am wrong but this how I would use the array for the teleport service yes?
TeleportService:TeleportToPrivateServer(1234567890,code,PlayersToTeleport)

3 Likes

I’m on mobile but I’ll do the best I can to explain. First off, yes that’s how you use the teleport service as long as “code” is the reserved server access code returned by TeleportService:ReserveServer.

To remove a player from the array you need to first find the key that the player is stored in and then use table.remove, like so:

for Key,Player in next,PlayersToTeleport do
     if Player == PlayerToRemove then
          table.remove(PlayersToTeleport,Key)
          break
     end
end
4 Likes

Thank you to @korky5000 for providing me with this piece of code to add players to a table, it successfully worked in teleporting the people in the table via teleport service. Thank you again.
If there are any more suggestions or feedback, i’d be glad to hear about it!

Also just to confirm, if a player leaves while they are in the table, will this break anything with the teleport service or the script?

I’m not sure, it’s probably best practice to just remove them from the table when they leave the game.

local Players = game:GetService("Players")

Players.PlayerRemoving:Connect(function(Player)
     for Key,OtherPlayer in next,PlayersToTeleport do
          if OtherPlayer == Player then
               table.remove(PlayersToTeleport,Key)
               break
          end
     end
end)
2 Likes