How do you teleport all players to an random game?

Hey do you know if theres an way to an random game id, from like any id and maybe even setting it such as the minum is uh… 95000, then the max is 500000.
Is there an way to teleport players to an random game?

This will be usefull for the command
;teleport all random

I might make the command and somehow put it in hd admin

just use math.random()?

and then use TeleportService to see if it exists by teleporting in a pcall.

I don’t know what an p(player)call is?

Perhaps, store all of the place ids on a array and then loop through all the existing players, and select a random index from the place id array and teleport them with TeleportService

example: (apologies for any typos, I am writing this on mobile)

local Players = game:GetService("Players")
local TeleportService = game:GetService("TeleportService")
local placeids = { --// fill the ids here
    01;
    02;
    --// etc
}

local function TeleportAllPlayersRandomly()
    for _, plr: Player in pairs(Players:GetPlayers()) do
        local success, err, tries, maxretry = false, "", 0, 3
        local selidx = math.random(1, #placeids)
        repeat
            success, err = pcall(function()
                TeleportService:Teleport(plr, tonumber(placeids[selidx]))
            end)
        until success or tries <= maxretry
    end
end

I don’t know what an array is [30]

Oh yeah, using math.random() wont work, because it will do an place id that would be restricted such as, place id, lets say… 505 or 456

Pcall is basiaclly just a way to wrap code in a function without erroring.

It also returns a value whether there was an error inside the function.

local TeleportService = game:GetService('TeleportService')

local function TeleportToRandomPlace(players)
  local randomid = math.random(95000, 500000)

  local success = pcall(function()
    TeleportService:TeleportAsync(randomid, players)
  end)
  if success then
    print('Players were teleported to place '..randomid)
  else
    return TeleportToRandomPlace(players)
  end
end

TeleportToRandomPlace(game.Players:GetPlayers())
1 Like

Array is an type of table, like:

local array = {1, "string", true, false, function() end, Instance.new("Part")}

Tried using this: local teleportservice = game:GetService(“TeleportService”)
–local game_1 = teleportservice.Game
local placeID = math.random()
local canteleport = true

game.Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(msg)
		--if msg == "Hello"
		if msg == "/teleport random place" then
			--teleportservice.ca
			teleportservice:Teleport(placeID)
		end
		-- do stuff with msg and player
	end)
end)

And yes, I did copy the roblox if chatted thing, cause theres legit no point of typing it out

Edit: it didnt work

local teleportservice = game:GetService("TeleportService")
--local game_1 = teleportservice.Game
local placeID = math.random()
local canteleport = true


game.Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(msg)
		--if msg == "Hello"
		if msg == "/teleport random place" then
			--teleportservice.ca
			teleportservice:Teleport(placeID)
		end
		-- do stuff with msg and player
	end)
end)

Well firstly, youre doing math.random() with no parameters which would get you a value inbetween 0 and 1

Secondly You’re not setting any players to be teleported

And also you’re using Teleport which has been superceded by TeleportAsync (just a small detail)

Changes:

local placeID = math.random(1,99999999)
teleportservice:TeleportAsync(placeID, {player})