Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to teleport back and forth??

Asked by 5 years ago

Okay, so let me explain what I wanted. And what the problem is, before I present the script. Okay so I made a gui with a text button. And it is supposed to take you from the lobby to the game, then after it does the text changes from "Game" to "Lobby", and when you click it again it is supposed to take you back to the lobby, then repeat this. The problem is that when you click it takes you to the game, then text changes to "Lobby" but when you click, it doesn't take you to the lobby part I am using the positions of the parts and idk if it is how I am doing it or not. And I am wondering how will I make it repeat? I've been working on it for a while, and I'd appreciate if anyone has a idea or clue how to fix this? Or make it better??

SCRIPT

local ingame = false
    wait(.5)
    game.Players.LocalPlayer.PlayerGui.ScreenGui.TextButton.Text = ("Game")
    script.Parent.MouseButton1Click:Connect(function()
    game.Players.LocalPlayer.character.Torso.CFrame = CFrame.new(workspace.gameT.position)
    if ingame == false then
        wait(.1)
        game.Players.LocalPlayer.PlayerGui.ScreenGui.TextButton.Text = ("Lobby")
        script.Parent.MouseButton1Click:Connect(function()
        game.Players.LocalPlayer.character.Torso.CFrame = CFrame.new(workspace.lobbyT.Position)
        wait(.1)
        end)
        end
    end)

here is a gif of what happens too btw: https://gyazo.com/11489be121de4cff760bfb71b6a9d695

0
Your scripting is very messy. I recommend using 1 MouseButton1Click event over using 2 with one wrapping around the other and also you didn't change the ingame boolean poke7667 142 — 5y
0
Sorry, I'm not that well at scripting. And I didn't realize how messy it was with the boolean lol. Thank you for the recommendation panichub 29 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago

Workspace --> gameT (part) --> lobbyT(part)

StarterGui --> ScreenGui -->TextButton --> LocalScript

The button's text has started with "Teleport to game"

Here is the script I made:

-- Variables
local player = game:GetService("Players").LocalPlayer
local button = script.Parent
local switch = true
gameLocation = game:GetService("Workspace").gameT.Position -- Make sure your location is set the game part you want to teleport to
lobbyLocation = game:GetService("Workspace").lobbyT.Position -- or lobby part

function clicked()
    -- Changes the text, moves the character to the position of the part, and changes the boolean to the opposite boolean depending if the boolean is true or false
    if (switch) then
        button.Text = "Teleport to lobby"
        player.Character.HumanoidRootPart.CFrame = CFrame.new(gameLocation)
        switch = false
    else
        button.Text = "Teleport to game"
        player.Character.HumanoidRootPart.CFrame = CFrame.new(lobbyLocation)
        switch = true
    end
end

-- When the button is clicked, run the function clicked()
button.MouseButton1Click:Connect(clicked)
0
Thank you a lot, I apologize for my messiness in the script. I completely forgot about the Getservice and I should make variables instead of just throwing it all in. panichub 29 — 5y
0
To be honest, I don't know why people use GetService lol. Most of the time I just use game.Workspace, etc. skate992 57 — 5y
0
Lol I'm just new to scripting, I at least had the right idea?? And honestly I am sick right now, and was on medicine when I wrote that lmao. But, thank you a lot! panichub 29 — 5y
0
No problem. skate992 57 — 5y
Ad

Answer this question