Reserving server for teleporting not working (Solved)

so i made a script where it can reserve a server so not everyone can join it, and then teleport the people, but for some reason I can’t get teleported, can someone help?

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerScriptService = game:GetService("ServerScriptService")
local TeleportService = game:GetService("TeleportService")

local SafeTeleport = require(ServerScriptService.SafeTeleport)

local elevator = script.Parent
local config = elevator.Config
local playersWaiting = {}
local countdownRunning = false
local moving = false

local function Setup()
	playersWaiting = {}
	moving = false
	local gui = elevator.Screen.SurfaceGui
	gui.Title.Text = #playersWaiting .. "/" .. config.MaxPlayers.Value .. " Players"
	gui.Status.Text = "Waiting for players..."
end

local function TeleportPlayers()
	local placeId = 16998676822
	local success, server = pcall(function()
		return TeleportService:ReserveServer(placeId)
	end)

	if success then
		local options = Instance.new("TeleportOptions")
		options.ReservedServerAccessCode = server
		SafeTeleport(placeId, playersWaiting, options)
		print("Finished teleport")
	else
		warn("Failed to reserve server:", server)
	end
end


local function MoveElevator()
	moving = true
	local gui = elevator.Screen.SurfaceGui
	for i, player in pairs(playersWaiting) do
		ReplicatedStorage:WaitForChild("MovingElevator"):FireClient(player)
	end
	gui.Status.Text = "Teleporting Players..."
	TeleportPlayers()
	task.wait(10)
	Setup()
end

local function RunCountdown()
	countdownRunning = true
	local gui = elevator.Screen.SurfaceGui
	for i = 10, 1, -1 do
		gui.Status.Text = "Starting in: " .. i
		task.wait(1)
		if #playersWaiting < 1 then
			countdownRunning = false
			Setup()
			return
		end
	end
	MoveElevator()
	countdownRunning = false
end

elevator.Entrance.Touched:Connect(function(part)
	local player = Players:GetPlayerFromCharacter(part.Parent)
	local gui = elevator.Screen.SurfaceGui
	local isWaiting = table.find(playersWaiting, player)

	if player and not isWaiting and #playersWaiting < config.MaxPlayers.Value and not moving then
		table.insert(playersWaiting, player)
		gui.Title.Text = #playersWaiting .. "/" .. config.MaxPlayers.Value .. " Players"
		if player.Character then
			player.Character:SetPrimaryPartCFrame(elevator.TeleportIn.CFrame)
		end
		ReplicatedStorage:WaitForChild("Elevator"):FireClient(player, elevator)
		if not countdownRunning then
			RunCountdown()
		end
	end
end)

ReplicatedStorage:WaitForChild("Elevator").OnServerEvent:Connect(function(player)
	local gui = elevator.Screen.SurfaceGui
	local isWaiting = table.find(playersWaiting, player)
	if isWaiting then
		table.remove(playersWaiting, isWaiting)
	end

	gui.Title.Text = #playersWaiting .. "/" .. config.MaxPlayers.Value .. " Players"

	if player.Character then
		player.Character:SetPrimaryPartCFrame(elevator.TeleportOut.CFrame)
	end
end)
1 Like

If it’s giving you 403 Forbidden, then your issue is that you are teleporting in Studio. And, you cannot teleport in Studio, you have to go to Roblox if you want to test your teleporting.

3 Likes

I already tried in game but didnt work

1 Like

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