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

Teleporting between a consistent place and another?

You may have seen a previous question I've asked about this issue, but this time I believe I am closer to finding the solution. However, this time, I have a problem that may be simple to solve, but I just can't see the solution.

I'm trying to make a GUI that can be pressed to bring a player to a sort of hub, and then pressed again to be returned to their previous location prior to the first press. The hub location is fixed at a part's CFrame, while the previous position is recorded before the function decides if the player is going to the hub or their previous location.

My problem is that the function goes through both conditions of the script, instead of only going through the true or false condition. I've tried using "else" instead of "if", moving the locations of "end", and moving the locations of one condition enabling another for the next use of the function, yet nothing has worked.

Here's a look at the script:

local Event = game.ReplicatedStorage.TeleEvent
local inHub = false
local HubWarp = workspace.HubWarp

Event.OnServerEvent:Connect(function(player)
		local oldpos = player.Character.HumanoidRootPart.CFrame
		local char = player.Character
			
	if inHub == false then
				inHub = true
	char.HumanoidRootPart.CFrame = HubWarp.CFrame
				print("Going to hub...")
	end
	
	if inHub == true then
			inHub = false
	char.HumanoidRootPart.CFrame = oldpos
		print("Going to previous location...")
	end
	
	end)

And the local script, if necessary:

Event = game.ReplicatedStorage.TeleEvent
player = game.Players.LocalPlayer

script.Parent.MouseButton1Click:connect(function()
	Event:FireServer(player)
	print("Fired")
end)

Additional information: The script is in ServerScriptService. The Local Script is in a ScreenGui. In output, all printable phrases are printed in one mouse click. It goes in the order of "Fired", "Going to hub...", "Going to previous location...". In-game, the player does not appear to move.

1 answer

Update: I've gotten the teleport to the hub to work, but now the character won't teleport to their initial position? Apparently, an elseif grouping it together made the initial part work, but the teleport back to their previous position does not do anything, though the position exists. I used a random CFrame to test if it recognizes a CFrame, and it does. What is incorrect?

Updated Script:

local Event = game.ReplicatedStorage.TeleEvent
local inHub = false
local HubWarp = workspace.HubWarp

Event.OnServerEvent:Connect(function(player)
        local char = player.Character
		local pos = player.Character.HumanoidRootPart.CFrame
		--I substituted this position for "CFrame.new(1,1,10)", and it seemed to work

    if inHub == true then
            inHub = false
    char.HumanoidRootPart.CFrame = pos
        print("Going to previous location...")

    elseif inHub == false then
                inHub = true
    char.HumanoidRootPart.CFrame = HubWarp.CFrame
                print("Going to hub...")


    end

    end)

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