Random player teleporter

Hello!
I was working on a script that would be able to pick a random player and teleport them to a certain location. Once I got that down I attempted to make it so the game would pick 2 different people and send them to 2 different locations. however, with my current scripting skill set, this is hard for me to accomplish without help.

wait(3)

local players = game.Players:GetPlayers()

local randomplayer = players[math.random(#players)]

local character = randomplayer.Character or randomplayer.CharacterLoaded:Wait()

character.HumanoidRootPart.Position = Vector3.new(62.35, 50.9, -0.95)

This is the code I currently have. Does anyone know how to make it pick 2 players and send them to 2 different locations?

2 Likes

Is the main issue the need to pick two unique random players to teleport?

Yes. Now that I’m thinking about it I could have just said that.

local Players = game:GetService("Players")

local function TeleportPlayerToPosition(player: Player, position: Vector3)
	if (not (typeof(player) == "Instance")) or (not (player.ClassName == "Player")) then return end
	if not (typeof(position) == "Vector3") then return end
	
	local character = player.Character
	if not character then return end
	
	character:PivotTo(position + character:GetPivot().Rotation)
end

local function GetRandomPlayers(count: number?)
	local randomPlayers = Players:GetPlayers()
	local playerCount = #randomPlayers
	
	if not (type(count) == "number") then return randomPlayers end
	
	while true do
		table.remove(randomPlayers, math.random(playerCount))
		
		playerCount -= 1
		
		if playerCount <= count then break end
	end
	
	return randomPlayers
end

local function TeleportPlayersToPosition(players: { Player }, position: Vector3)
	for _, player in players do
		TeleportPlayerToPosition(player, position)
	end
end

TeleportPlayersToPosition(GetRandomPlayers(2), Vector3.new(62.35, 50.9, -0.95))
1 Like

If you want you could create a gui with a button and paste this script in there, this will take two random players out of a list and send them to a random location.

-- lets grab a list of all the players in the game

local players = game:GetService("Players"):GetPlayers()

--let us create a function to pick two random players, in this case characters
local function pickRandomCharacters()
  local character1 = players[math.random(#players)].Character
  local character2 = players[math.random(#players)].Character
  return character1, character2
end

-- lets create a function that takes these two random characters/players w/ random tp
local function teleportCharacters(character1, character2)
  character1:MoveTo(Vector3.new(math.random(-100,100), 0, math.random(-100,100)))
  character2:MoveTo(Vector3.new(math.random(-100,100), 0, math.random(-100,100)))
end

-- this is a button you can make in a gui, to tp the players
local button = script.Parent

-- fires the button's left click mouse event to use the functions
button.MouseButton1Click:Connect(function()
  local character1, character2 = pickRandomCharacters()
  teleportCharacters(character1, character2)
end)

This script will randomly teleport the player themselves when they click the button

-- the button youre going to click
local button = script.Parent

-- the function that tps the players
button.MouseButton1Click:Connect(function()
	for _, player in pairs(game.Players:GetPlayers()) do
		player.Character:MoveTo(Vector3.new(math.random(-100, 100), math.random(-100, 100), 0))
	end
end)

wait(5)

Let me know if you have any questions, or if you want to teleport every player when they join to a random location. If you also want to teleport all players at the same time when the button is clicked, that can be done too. Cheers

2 Likes

Thank you so much! This was legit so helpful!

1 Like

With this method, it is possible to receive the two same random players.

You are correct about that my friend, but if the game has quite a few amount of players, say 20, is the possibility of the two same players being chosen still the same, or does it get slightly reduced?

The thing is that there is always a possibility.

You are correct in a way. Of course, you could say there is a possibility it could happen- even if that chance is 0.0001%. We could say there is a very low possibility in that happening. Either way, it is a possibility so you are correct, but if we take this reasoning and apply it to other things, like say, having your game visited by Telamon, there is always a possibility of it happening, even if it is 0.00001 percent. Cheers

Said possibility could be something that they don’t want to happen tho

What i would do is put all the players into a table and then remove them from the table if they are picked. And, of course, put them back in the table later, if you wish to do so

Thanks my man. Here’s a revised script that takes the two random players and make sure they are not teleported again. The Pickrandomcharacters function now has a repeat which loops until it finds two players that haven’t been teleported yet, it knows this if the player isn’t in the TeleportedPlayers table. It now adds teleported players to the TeleportedPlayers table to make sure they don’t teleport again once they have been teleported. Cheers

-- lets grab a list of all the players in the game
local players = game:GetService("Players"):GetPlayers()

-- table to store the teleported players
local teleportedPlayers = {}

--let us create a function to pick two random players, in this case characters
local function pickRandomCharacters()
  local character1, character2
  -- pick random players until we get two that haven't been teleported yet
  repeat
    character1 = players[math.random(#players)].Character
    character2 = players[math.random(#players)].Character
  until not teleportedPlayers[character1] and not teleportedPlayers[character2]
  return character1, character2
end

-- lets create a function that takes these two random characters/players w/ random tp
local function teleportCharacters(character1, character2)
  character1:MoveTo(Vector3.new(math.random(-100,100), 0, math.random(-100,100)))
  character2:MoveTo(Vector3.new(math.random(-100,100), 0, math.random(-100,100)))

  -- add the teleported characters to the teleportedPlayers table
  teleportedPlayers[character1] = true
  teleportedPlayers[character2] = true
end

-- this is a button you can make in a gui, to tp the players
local button = script.Parent

-- fires the button's left click mouse event to use the functions
button.MouseButton1Click:Connect(function()
  local character1, character2 = pickRandomCharacters()
  teleportCharacters(character1, character2)
end)

No need for a loop to insure no duplicates, as shown in my previous reply.

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