Randomized teleportation spot

So I wanted to make a game where there will be multiple doors. But I want the game to be always different. The game going to have doors that open and some that are locked. I want the door I want to teleport the person to be random. Ex. Instead of the person teleporting to only one designated place like this :arrow_down_small:

local TS = game:GetService("TeleportService")
local TweenService = game:GetService("TweenService")
local placeId = "8626423475"
local leaveGuiEvent = game.ReplicatedStorage.LeaveGuiEvent
local TransitionEvent = game.ReplicatedStorage.TransitionEvent
local list = {}
local gui = script.Parent.GuiPart.SurfaceGui
local billboard = script.Parent.billboardPart.billboardGui
local timer
local teleporting = false
local spawnTeleport = script.Parent.spawn

I want the teleport to be random. So like have it have been like this :arrow_down_small:

local TS = game:GetService("TeleportService")
local TweenService = game:GetService("TweenService")
local s=math.random(1,5)
if s == 1 then
local placeId = "8626423475"
elseif s == 2 then
local placeId = "7626423963" --random numbers, pretend these are real teleportation places 
elseif s == 3 then
local placeId = "3453463445"--random numbers
elseif s == 4 then
local placeId = "9345423963"--random numbers
elseif s == 5 then
local placeId = "4629826455"--random numbers
local leaveGuiEvent = game.ReplicatedStorage.LeaveGuiEvent
local TransitionEvent = game.ReplicatedStorage.TransitionEvent
local list = {}
local gui = script.Parent.GuiPart.SurfaceGui
local billboard = script.Parent.billboardPart.billboardGui
local timer
local teleporting = false
local spawnTeleport = script.Parent.spawn

using a lot of else if isn’t a good practice.

here is something instead:

local TS = game:GetService("TeleportService")
local Destination = {"1298198391","4728974198","93719700"} --Put the ID of the places that you want to be randomized. These are also random numbers

local placeId = Destination[math.random(1,#Destination)]--This will choose a random entry from the table above

Ok so I added the code in and this is how it’s looking

local TS = game:GetService("TeleportService")
local TweenService = game:GetService("TweenService")
local TS = game:GetService("TeleportService")
local Destination = {"8626423475","9204870915","9204891593"} --Put the ID of the places that you want to be randomized. These are also random numbers

local placeId = Destination[math.random(1,#Destination)]--This will choose a random entry from the table 
local leaveGuiEvent = game.ReplicatedStorage.LeaveGuiEvent
local TransitionEvent = game.ReplicatedStorage.TransitionEvent
local list = {}
local gui = script.Parent.GuiPart.SurfaceGui
local billboard = script.Parent.billboardPart.billboardGui
local timer
local teleporting = false
local spawnTeleport = script.Parent.spawn

local function updateGui()
	gui.Frame.players.Text = #list 
	billboard.Frame.players.Text = #list 
end

local function removeFromList(character)
	for i=1,#list do
		if list[i] == character.Name then
			table.remove(list,i)
			updateGui()
		end
	end
end

local function teleportPlayers()
	if #list > 0 then
		script.Parent.GuiPart.SurfaceGui.Frame.Status.Text = "TELEPORTING"
		script.Parent.GuiPart.SurfaceGui.Frame.Status.TextColor3 = Color3.new(1,0,0)
		local playersToTeleport = {}
		local teleportTime = 0
		for i=1,#list do
			if game.Players:findFirstChild(list[i]) then
				table.insert(playersToTeleport,game.Players:findFirstChild(list[i]))
				TransitionEvent:FireClient(game.Players:findFirstChild(list[i]))
			else
				table.remove(list,i)	
			end
		end 
		local code = TS:ReserveServer(placeId)
		teleporting = true
		TS:TeleportToPrivateServer(placeId,code,playersToTeleport)
		repeat wait() until #list <= 0
		script.Parent.GuiPart.SurfaceGui.Frame.Status.Text = "READY"
		script.Parent.GuiPart.SurfaceGui.Frame.Status.TextColor3 = Color3.new(34/255,255/255,20/255)
		teleporting = false
	end
end

script.Parent.Gate.Touched:Connect(function(hit)
	if hit.Parent:findFirstChild("Humanoid") then
		if teleporting == false then
			local char = hit.Parent
			local player = game.Players:FindFirstChild(char.Name)
			local alreadyExists = false
			
			for i=1,#list do
				if list[i] == char.Name then
					alreadyExists = true
				end
			end
			
			if alreadyExists == false then
				if #list < 1 then
					table.insert(list,char.Name)
					char.PrimaryPart.CFrame = spawnTeleport.CFrame
					updateGui()
					leaveGuiEvent:FireClient(player)
				end
				
				player.CharacterRemoving:connect(function(character)
					removeFromList(character)
				end)
			end
			
		end
	end
end)

leaveGuiEvent.OnServerEvent:Connect(function(player)
	if player.Character then
		player.Character.HumanoidRootPart.Anchored = false
		wait()
		player.Character.Humanoid.Jump = true
		wait()
		player.Character:MoveTo(game.Workspace.leaveRoomPart.Position)
		removeFromList(player.Character)
	end
end)

while wait() do
	timer = 20
	for i=1,timer do
		timer = timer - 1
		gui.Frame.time.Text = timer
		billboard.Frame.time.Text = timer
		wait(1)
	end
	teleportPlayers()
end

The output is telling me that the
HTTP 403 (Forbidden) - Server - TouchGateScript:44
aka line that says “local code = TS:ReserveServer(placeId)”
what do I do

nvm, I just had to readjust the rest of the placeID and it worked fine. Thanks

1 Like