Im making a teleport step on button to teleport

so i have made a working teleport button and walk on block to see the gui the gui does work but how would I make it so when you click the button it will teleport you to the location and get rid of the ui with doing this it has broken the teleport system

Code:

wait(1)
player = game.Players.LocalPlayer
button = script.Parent
local debounce = false

function teleport()
if not debounce then
debounce = true
LowerTorso = player.Character.LowerTorso
LowerTorso.CFrame = game.Workspace.Place1.CFrame
end
end

button.MouseButton1Click:Connect(teleport)
while true do wait()
debounce = false
wait(1)
end

else if
script.Parent.MouseButton1Click:Connect(function()
script.Parent.Parent.Visible = false
end)
end

1 Like

The classic alvinblox wait to fix code :expressionless:

This code is extremely messy and has loads of redundancies, and in all honesty I don’t exactly know what most of it does due to it’s weird hierarchy structure, so I just cleaned it up.

--// Variables
local player = game.Players.LocalPlayer
local button = script.Parent
local debounce = false

--// Functions
local function teleport()
    if not debounce then
        debounce = true
        local root = player.Character:WaitForChild("HumanoidRootPart")
        root.CFrame = game.Workspace.Place1.CFrame --// Replace with PivotTo
        task.wait(1)
        debounce = false
    end
end

--// Events
button.MouseButton1Click:Connect(function()
    teleport() -- Idk the reason for the function either, you don't repeat it 
    -- UI stuff here
end)

Hope this helps.

1 Like

wait(1)
player = game.Players.LocalPlayer
button = script.Parent
local debounce = false

function teleport() --Defines where the player teleports in the workspace
if not debounce then
debounce = true
LowerTorso = player.Character.LowerTorso
LowerTorso.CFrame = game.Workspace.Place1.CFrame
end
end

button.MouseButton1Click:Connect(teleport) --Teleport Button
while true do wait()
debounce = false
wait(1)
end

else if
script.Parent.MouseButton1Click:Connect(function() --Then to close the gui
script.Parent.Parent.Visible = false
end)
end

I know what your code does but the way it is laid out makes it hard to understand, plus it is poorly written with all the over-engineering. The code I wrote is cleaner and only needs you to put in the UI, which even still shouldn’t be in the event as it’s a separate event.

Im not a good scripter i used a tutorial on youtube for this if you want it i can give you the tutorial

I’m not sure what you’re not understanding. I have rewritten the code into a more readable and working state for you. The tutorial you watched is either extremely wrong or you have poorly ported it.

Yes, Sorry for the late reply how would I order the Gui i have tried myself but it gives errors again im not a good scripter