Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to teleport back and forth??

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

2 comments

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

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

1 answer

✓ Accepted answer

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)

4 comments

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.

0

To be honest, I don't know why people use GetService lol. Most of the time I just use game.Workspace, etc.

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!

0

No problem.

This discussion is preserved as it was. New answers, comments, and votes are closed.