Help with Teleport

Hello!

I am working on a Teleport Gui but the problem is I had to use two ImageButtons for it to work, I was wondering if I could just change the Button without using two gui buttons but one

I have tried using the if statements and other forms of code, but it wouldn’t work!

Here is the script I am currently using:

local Button = script.Parent
local Sound = script.Parent.Click

function ChangeButton()
	Button.Image = "rbxassetid://6807196325"
	Button.ImageColor3 = Color3.new(255, 255, 255)
	script.Parent.Parent.ButtonInfo.Text = "Teleport back to Lobby"
end

function ChnageButton2()
	Button.Image = "rbxassetid://7117826645"
	Button.ImageColor3 = Color3.new(0, 0, 0)
	script.Parent.Parent.ButtonInfo.Text = "Teleport into the Testing Room"
end

Button.MouseButton1Click:Connect(function()
	local Player = game.Players.LocalPlayer
	Player.Character.HumanoidRootPart.CFrame = CFrame.new(21.43, 3, -47.947)
	Sound:Play()
	ChangeButton()
	
end)

Button.MouseButton1Click:Connect(function()
	local Player = game.Players.LocalPlayer
	Player.Character.HumanoidRootPart.CFrame = CFrame.new(54.83, 2.856, -47.947)
	Sound:Play()
	ChnageButton2()

end)

Button.MouseEnter:Connect(function()
	Button:TweenSize(
		UDim2.new(0.087, 0, 0.132, 0), 
		Enum.EasingDirection.In, 
		Enum.EasingStyle.Linear, 
		0.1, 
		true
	)
	script.Parent.HoverSound:Play()
	script.Parent.Parent.ButtonInfo.Text = "Teleport into the Testing Room"
	script.Parent.Parent.ButtonInfo.Visible = true
end)

Button.MouseLeave:Connect(function()
	Button:TweenSize(
		UDim2.new(0.081, 0, 0.121, 0), 
		Enum.EasingDirection.Out, 
		Enum.EasingStyle.Linear, 
		0.1, 
		true
	)
	script.Parent.Parent.ButtonInfo.Visible = false
end)

I have used functions but it wouldn’t work, the functions are working fine but the Teleporting wouldn’t, once I clicked it, it would teleport me but when I click it again it’s supposed to teleport me to “54.83, 2.856, -47.947” but it would just keep teleporting me to the first position.

There were no errors in the output box. What could I do?

Yes, you can accomplish this with one button.

-- Its better to define things like the player and character at the top
-- so you can always reference them again later from anywhere in the script
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()

local Button = script.Parent
local Sound = sript.Parent.Click

-- here we're saving the two locations and 
-- should be able to use Vector3's since we want the position data
local teleportLocation =  Vector3.new(21.43, 3, -47.947)
local lobbyLocation = Vector3.new(1,1,1) -- Position for getting back to the lobby

-- We can create a Boolean to keep track of where the player is
-- True means we're in the lobby and False means we're elsewhere, in this case
local inLobby = true 

Button.MouseButton1Click:Connect(function()
    -- Now we can check if we're in the lobby or not 
    -- and teleport to the proper location
    if inLobby then
        character:MoveTo(teleportLocation)
        inLobby = false

        -- Edit the rest of the button
        Button.Image = "rbxassetid://6807196325"
        Button.ImageColor3 = Color3.new(255, 255, 255)
        script.Parent.Parent.ButtonInfo.Text = "Teleport back to Lobby"
    else
        character:MoveTo(lobbyLocation)
        inLobby = true

        -- Edit the rest of the button
        Button.Image = "rbxassetid://7117826645"
        Button.ImageColor3 = Color3.new(0, 0, 0)
        script.Parent.Parent.ButtonInfo.Text = "Teleport into the Testing Room"
    end

    Sound:Play()
end)

Instead of setting the CFrame of the root part I used MoveTo() which is a better method for changing the location of a model. You can find more on that here: Model | Roblox Creator Documentation

2 Likes

Thank you so much!

It was explained very well! I have been learning about scripting and I never knew about the MoveTo() function! It works perfectly!

1 Like