Teleporting back and forth while being stuck in a part

I just figured out how to script teleportation but I still haven’t perfected it.Therefore, I need the forum’s help.
I built two portals and made the scripts:

Portals

47%20AM

Script

But when I teleport in play testing I get stuck in the part and keep teleporting back and forth (over and over again none stop).

Video

Use a debounce so it’ll be on a cool down and that won’t happen.

2 Likes

This has happened to me before too, I would suggest to add a offset to your designated CFrame so when the player gets teleported, they don’t get immediately teleported back. You can also just add a debounce/cooldown between portal teleports. Hope this helps!

1 Like

How would I do that? Sorry, I work best with visuals.

Oh, that’s fine, in your teleport CFrame, tweak the last part which is “-138” to maybe “-141”. Just mess with the last value in the CFrame, and it should work.

1 Like

It looks like you have cancollide on. Maybe you can turn that off?

1 Like

The reason this happens is your code gets stuck in an infinite loop because the Touched event fires multiple times. A “debounce” helps fix this issue:

Edit: Also yeah make sure your teleports have CanCollide set to false or your character will get stuck in the parts!

1 Like

I tested that out and it still teleports me back and forth. Cancollide isn’t the solution sadly

1 Like

Okay, this works a little better. I only teleport back and forth for a few times

Then i would definitely use a debounce, and maybe add a ui fade so it hides the teleport. Just a suggestion :slight_smile:

1 Like

Perfect, that just means you gotta tweak it a little more, This is what the game “Portal” does, it offsets the player when they enter the portal and come out.

Here’s a video of what it is doing now:

Video

Did you add a debounce to both teleporters? It seems one works, the other doesn’t.

1 Like

I tweaked the other script too but it still keeps teleporting me back to my original position

I haven’t added a debounce yet. That’s my next option

1 Like

Yea, just tweak it a bit more, or you can add a debounce, but you said you work best with visuals! So, just keep tweaking it.

1 Like

As they said before make a debounce. The procedure works like:

local debounce = false -- creates a debounce variable
local cooldown = 2 -- time until the teleporter can be used again
script.Parent.Touched:Connect(function(hit) -- fires when the player touches the teleporter
local humanoid = char.Humanoid
local teleport = script.Parent.TpPart
if humanoid and debounce == false then
    debounce = true -- Sets debounce to true
	local player = game.Players:GetPlayerFromCharacter(char)
	game.ReplicatedStorage.ChangeSky:FireClient(player)
	game.Workspace[player.Name].HumanoidRootPart.CFrame = teleport.CFrame
    wait(cooldown) -- wait until teleporter can be used again
    debounce = false -- reactivate the teleporter
end
end)
1 Like

Yeah, the tweaking isn’t working. I’ve changed the position numbers until they are 10 numbers off (higher or lower) from original

Do I put this script at the end of my script or do I totally rewrite the script and do your example?

Alright, just add a debounce! You can follow the official roblox guide here: Debounce Patterns | Documentation - Roblox Creator Hub

1 Like