Teleporting back and forth while being stuck in a part

Oh yes u are right. This is because I copied the script from one of my games

1 Like

This is after I added local char = hit.Parent

I would use @Dracolyx script, and then add a debounce to it.

Edit: Even better, he just added one! Use his!

1 Like

Yeah I just edited it adding a debounce so it’s all good now.

I just used it, but I didn’t teleport…Is that because I should have put the position in the code here:

You have to add a part named “TpPart” into workspace where you want the player to teleport, and locate the front surface of the part and rotate it towards the direction you want the player to be tped to.

1 Like

So, in the code could I just name the part portal instead of TpPart? Do you mind sending me a picture of what you mean? Thx

Open properties in studio, select the “Portal” part in workspace (rename TpPart to Portal if you want to change it), and scroll down the properties until you see surfaces. Select ‘FrontSurface’ and it will highlight the part in yellow. That is where the player will teleport in front of the part.

1 Like

The problem here is you are teleporting the player directly to the position of the other portal, which then just teleports them back and vice-versa.

To fix this, the two previous solutions should work fine: teleport the player with a slight offset and a debounce. However, a debounce won’t be useful unless you’re only using one script. Here, let’s take your code, edit it slightly and put it within on script rather than two:

Script can be placed anywhere since it references the portals from workspace.

local debounce = false

local portal1 = workspace.Portal -- path to the first portal
local portal2 = workspace.Portal2 -- path to second portal

portal1.Part.Touched:Connect(function(hit)
    local root = hit.Parent:FindFirstChild("HumanoidRootPart")
    if (not debounce) and hit.Parent:FindFirstChildOfClass("Humanoid") and root then -- check debounce in false and check for important parts of character
        debounce = true
        delay(1, function() debounce = false end) -- set debounce back to false in 1 second
        root.CFrame = CFrame.new(portal2.Part.CFrame.Position + Vector3.new(0,0,5))
        -- move the player to the other portal, but a little to the side
        -- edit the "Vector3.new(0,0,5)" to change the offset if it's too far or too close.
    end
end)

-- lets do the same thing for portal2!
portal1.Part.Touched:Connect(function(hit)
    local root = hit.Parent:FindFirstChild("HumanoidRootPart")
    if (not debounce) and hit.Parent:FindFirstChildOfClass("Humanoid") and root then
        debounce = true
        delay(1, function() debounce = false end)
        root.CFrame = CFrame.new(portal2.Part.CFrame.Position + Vector3.new(0,0,5))
    end
end)

You may need to edit the offsets for your parts but you should be able to get it right with a few trials.
Try:

Vector3.new(0,0,-5)
or
Vector3.new(5,0,0)

instead if it doesn’t work!

Edit: Used Vectors instead of CFrames as may be easier to understand

1 Like

Thanks everyone for trying to help me out! It’s been a long thread but @Nova_MrRoyal helped me through team create.

The solution we made looks like this:
Image of the workspace:

TeleportPost

The two scripts:

Number 1:

local tp = script.Parent.TpPortal2
local debounce = false

script.Parent.Touched:Connect(function(hit)
    local char = hit.Parent
    if debounce == false then
	    if char:FindFirstChild("Humanoid") then
		    char:moveTo(tp.Position)
		    wait(1)
	    end
	    debounce = false
    end
end)

Number 2:

local tp = script.Parent.TpPortal1
local debounce = false

script.Parent.Touched:Connect(function(hit)
    local char = hit.Parent
    if debounce == false then
	    if char:FindFirstChild("Humanoid") then
		    char:moveTo(tp.Position)
		    wait(1)
	    end
	    debounce = false
    end
end)
2 Likes