Teleport is failing with no error when testing this code in actual game...help?

THE CODE

local playButton = script.Parent
local mouse = game.Players.LocalPlayer:GetMouse()
local GuiObject = script.Parent

playButton.MouseButton1Click:Connect(function()
script.Parent.Parent.Frame.Active = false
script.Parent.Visible = false
script.Parent.Parent.Frame.BackgroundTransparency = 1
for i = 1,0,-0.01 do
script.Parent.Parent.Frame.BackgroundTransparency = i
wait(0.01)

game:GetService("TeleportService"):Teleport(4502575730)
print ("Teleporting Player!")

end
end)

THE PROBLEM

Every time I test out this code in the actual game (not in-studio) it will successfully initiate the teleport, but then my cursor freezes and the game starts loading and eventually crashes. I am really confused and would appreciate it if someone could help me out. There are no error codes/messages.

Thank you, and be safe.

Game Settings > Security > Teleport

enable it.

The issue here is that you scoped your Teleport function within your for loop. The function will run 100 times almost instantly with a small wait, causing it to crash. What you should do is either drop your function one line, so it’s under the end or do it before your for loop.

local playButton = script.Parent
local mouse = game.Players.LocalPlayer:GetMouse()
local GuiObject = script.Parent

playButton.MouseButton1Click:Connect(function()
script.Parent.Parent.Frame.Active = false
script.Parent.Visible = false
script.Parent.Parent.Frame.BackgroundTransparency = 1

for i = 1,0,-0.01 do --runs 100 times
   script.Parent.Parent.Frame.BackgroundTransparency = i
   wait(0.01)
end
game:GetService("TeleportService"):Teleport(4502575730) -- Runs after your for loop is finished
print ("Teleporting Player!") 
end)

Alternatively call the function before the for loop. If you wanted your transparency for loop to run simultaneously to your teleport function, you could actually create a function for your transparency and call it at the same time as your teleport.

1 Like

Thank you very much, I appreciate the help. Stay safe out there.

1 Like