Random player teleport

Hello. Im trying to make a script where it gets a random player and teleports it to a part. After 10 seconds it will make the random player go back to spawn and it will grab another random player and so on. Asked AI but everything fails. Any help or ideas?

local Players = game:GetService("Players")

local DelayBetweenGrabs = 10 -- The duration between teleports
local TeleportPart = workspace.Part -- The part to teleport the player to
local SpawnPart = workspace.SpawnLocation -- Make this a part where you want the player to return to

local function SelectRandomPlayer() -- Function for getting a random player
	repeat wait() until Players:GetPlayers() ~= 0 -- New servers, wait for the first player to join
	local Players = Players:GetPlayers()
	local NumberOfPlayers = #Players
	return Players[math.random(1,NumberOfPlayers)]
end

while task.wait() do
	local Player
	
	repeat
		Player = SelectRandomPlayer() -- Returns a random player
	until Player.Character -- Make sure the player has a character (is loaded in)
	
	local Character = Player.Character -- Get Character
	
	Character:PivotTo(TeleportPart.CFrame) -- Move player to teleport part
	task.wait(DelayBetweenGrabs)
	Character:PivotTo(SpawnPart.CFrame) -- Move player back
end

Make the teleport part and spawn part whatever parts you’re using for each respectively then you should be good.

1 Like

Thanks! Where should i put it in?

You can use a server script in server script service :slight_smile: as long as it’s happening on the server, otherwise other clients won’t see the players teleporting.

1 Like