Teleporter Spawning, Not Teleporting

  1. What do you want to achieve? I’m trying to make a portal that has a % chance to spawn, and when it spawns, it has another % (math.random) chance that decides what game it teleports you to. For example, if the math.random sent out 1, it would send you to a baseplate, and if it sent out 2, it would send you to a house game.

  2. What is the issue? The portal spawns, but it doesn’t select the place it should teleport to, and also therefore makes it so the teleport doesn’t work. (Note: in the code below there’s only 1 option for where it can teleport, if this may be causing the problem tell me.)

local portal = game.ServerStorage.SpecialPortal
local prompt = portal.QueuePrompt
local GameId = nil


while portal.Parent == game.ServerStorage do
wait(3)
	local spawnChance = math.random(1,5)
if spawnChance == 1 then
	portal.Parent = workspace
	print("Portal spawned")
wait(10)
	portal.Parent = game.ServerStorage
	print("Portal despawned")
else
		print("Portal did not spawn")
	end
end

while portal.Parent == game.Workspace do
	wait(1)
	local chance = math.random(1,1)
	print(chance)
if chance == 1 then
	GameId = 11767601212
		print("Portal goes to: The Middle")
	else
		
	end
end
  1. What solutions have you tried so far? I have looked on multiple devforum posts and tried to use and edit the code above, but none of it has worked. I would appreciate some help.

Are you setting the PlaceId to the Portal? looking at the code, you only appear to be getting the PlaceId. not Applying it to this "Portal"

There is teleporting code that uses the GameId to teleport you to it, but I didn’t include that because I know it works. (Except for in this case, of course.)

Can you at least provide it?

I would help

This is what I use for all teleports in the game. (Some things in this may not be needed, like the PlayersInQueue, that is because I just copied the code from my matchmaking portals.)

local TPS = game:GetService("TeleportService")

local PlayersInQueue = {}

local MinPlayers = 1
local MaxPlayers = 1

local isMatchmaking = false

function Matchmake()
	if isMatchmaking == false then
		isMatchmaking = true

		repeat wait()
			for i,v in pairs(PlayersInQueue) do
				game.ReplicatedStorage.UpdateStatus:FireClient(v,"Waiting for "..tostring(MinPlayers).." players to join the Queue.")
			end
		until #PlayersInQueue == MinPlayers

		--Enough players to start!

		for i=0,15,1 do
			if #PlayersInQueue == 0 then
				isMatchmaking = false

				--Players have left the game!
				return
			end

			if #PlayersInQueue == MaxPlayers then
				break
			end

			for _,value in pairs(PlayersInQueue) do
				game.ReplicatedStorage.UpdateStatus:FireClient(value,"Teleporting...")
			end

			wait(1)
		end

		--Finished waiting, teleporting players now!

		local Server = TPS:ReserveServer(GameId)
		TPS:TeleportToPrivateServer(GameId,Server,PlayersInQueue)

		wait(3)

		PlayersInQueue = {}
		isMatchmaking = false
	end
end

portal.QueuePrompt.Triggered:Connect(function(player)
	if #PlayersInQueue == MaxPlayers then return end
	if not table.find(PlayersInQueue,player) then
		table.insert(PlayersInQueue,player)
	end
	Matchmake()
end)

game.Players.PlayerRemoving:Connect(function(player)
	for i,v in pairs(PlayersInQueue) do
		if v.Name == player.Name then
			table.remove(PlayersInQueue,i)
		end
	end
end)

Your first while loop is never ending. Your condition is if the portal.Parent == game.ServerStorage which will always be true so you won’t ever get to the code in the second while loop. Not sure if this is exactly what you want but you could try something like this:

local portal = game.ServerStorage.SpecialPortal
local prompt = portal.QueuePrompt
local GameId = nil

spawn(function()
	while portal.Parent == game.ServerStorage do
		wait(3)
		local spawnChance = math.random(1,5)
		if spawnChance == 1 then
			portal.Parent = workspace
			print("Portal spawned")
			wait(10)
			portal.Parent = game.ServerStorage
			print("Portal despawned")
		else
			print("Portal did not spawn")
		end
	end
end)

while task.wait(1) do
	if portal.Parent == game.Workspace then
		local chance = math.random(1,1)
		print(chance)
		if chance == 1 then
			GameId = 11767601212
			print("Portal goes to: The Middle")
		else

		end
	end
	
end

It prints where the portal goes, but it keeps printing over and over, and when you try to actually teleport it doesn’t work. It also still keeps printing if I try to use a debounce.