Why my teleport script doesn't work?

Hello.
So, i did that teleport script, when you touch a part, there is a 30 second until you teleport to the other game, but for some reason, it is giving a HTTP 403 (Forbidden) error. On the line 42

Here is the code:

local TeleportPart = script.Parent
local PlaceID = 17067698002
local TransitionEvent = game.ReplicatedStorage.EventFolder.TransitionEvent

local TeleportService = game:GetService("TeleportService")
local PlayerService = game:GetService("Players")

local BillboardGui = TeleportPart:FindFirstChild("PlayersIn")
local PlayerText =  BillboardGui:FindFirstChild("PlayerText")
local TimerText = BillboardGui:FindFirstChild("TimerText")

local MaximumPlayerCount = 4
local Players = {}
local IsTeleporting = false

local function Refresh()
	PlayerText.Text = #Players.."/"..MaximumPlayerCount
end

local function RemovePlayer(Player)
	for i = 1, #Players do
		if Players[i] == Player.Name then
			table.remove(Players, i)
			Refresh()
		end
	end
end

local function TeleportPlayer()
	if #Players > 0 then
		local TeleportPlayers = {}
		
		for i = 10, #TeleportPlayers do
			if game.Players:FindFirstChild(Players[i]) then
				table.insert(TeleportPlayers, game.Players:FindFirstChild(Players[i]))
				TransitionEvent:FireClient(game.Players:FindFirstChild(Players[i]))
			else
				table.remove(Players, i)
			end
		end
		
		local Code = TeleportService:ReserveServer(PlaceID)
		
		IsTeleporting = true
		TeleportService:TeleportToPrivateServer(PlaceID, Code, TeleportPlayers)
		
		repeat task.wait()
				
		until #Players <= 0
		
		IsTeleporting = false
	end
end

TeleportPart.Touched:Connect(function(OtherPart)
	if OtherPart and OtherPart.Parent:FindFirstChild("Humanoid") then
		if IsTeleporting == false then
			local Player = PlayerService:GetPlayerFromCharacter(OtherPart.Parent)
			local Character = Player.Character or Player.CharacterAdded:Wait()
			local InParty = false
			
			for i = 1, #Players do
				if Players[i] == Character.Name then
					InParty = true
				end
			end
			
			if InParty == false and #Players < MaximumPlayerCount then
				table.insert(Players, Character.Name)
				task.wait(0.25)
				Refresh()
			end
			
			Player.CharacterRemoving:Connect(function(Character)
				RemovePlayer(Character)
			end)
		end
	end
end)

TeleportPart.TouchEnded:Connect(function(OtherPart)
	if OtherPart then
		local Player = PlayerService:GetPlayerFromCharacter(OtherPart.Parent)
		RemovePlayer(Player)
	end
end)

while task.wait(1) do
	local Timer = 30
	
	for i = 1, Timer do
		Timer = Timer - 1
		TimerText.Text = Timer
		task.wait(1)
	end
	
	TeleportPlayer()
end

Are you trying this in-game or in Studio? Have you checked game permissions?

Yeah, i tried in studio and in-game, and also enabled HTTPs request. But in-game it gives a diferent error.

There is a few logic errors here … without testing, here is a re-wright.

local TeleportPart = script.Parent
local PlaceID = 17067698002
local TransitionEvent = game.ReplicatedStorage.EventFolder.TransitionEvent

local TeleportService = game:GetService("TeleportService")
local PlayerService = game:GetService("Players")

local BillboardGui = TeleportPart:FindFirstChild("PlayersIn")
local PlayerText = BillboardGui:FindFirstChild("PlayerText")
local TimerText = BillboardGui:FindFirstChild("TimerText")

local MaximumPlayerCount = 4
local Players = {}
local IsTeleporting = false

local function Refresh()
    PlayerText.Text = #Players .. "/" .. MaximumPlayerCount
end

local function RemovePlayer(Player)
    for i = 1, #Players do
        if Players[i] == Player.Name then
            table.remove(Players, i)
            Refresh()
            break
        end
    end
end

local function TeleportPlayer()
    if #Players > 0 then
        local TeleportPlayers = {}
        for i = 1, #Players do
            local Player = game.Players:FindFirstChild(Players[i])
            if Player then
                table.insert(TeleportPlayers, Player)
                TransitionEvent:FireClient(Player)
            end
        end
        
        local Code = TeleportService:ReserveServer(PlaceID)
        IsTeleporting = true
        TeleportService:TeleportToPrivateServer(PlaceID, Code, TeleportPlayers)
        IsTeleporting = false
        Players = {}
        Refresh()
    end
end

TeleportPart.Touched:Connect(function(OtherPart)
    if OtherPart and OtherPart.Parent:FindFirstChild("Humanoid") then
        if not IsTeleporting then
            local Player = PlayerService:GetPlayerFromCharacter(OtherPart.Parent)
            local Character = Player.Character or Player.CharacterAdded:Wait()
            local InParty = false

            for i = 1, #Players do
                if Players[i] == Character.Name then
                    InParty = true
                    break
                end
            end

            if not InParty and #Players < MaximumPlayerCount then
                table.insert(Players, Character.Name)
                task.wait(0.25)
                Refresh()

                Player.CharacterRemoving:Connect(function()
                    RemovePlayer(Player)
                end)
            end
        end
    end
end)

TeleportPart.TouchEnded:Connect(function(OtherPart)
    if OtherPart and OtherPart.Parent:FindFirstChild("Humanoid") then
        local Player = PlayerService:GetPlayerFromCharacter(OtherPart.Parent)
        RemovePlayer(Player)
    end
end)

while true do
    local Timer = 30
    for i = 1, Timer do
        TimerText.Text = tostring(Timer - i)
        task.wait(1)
    end
    TeleportPlayer()
end

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