Problem Teleporting

I’m trying to make a teleporter for my story game but i’m new to scripting. Using proxymityprompt and 2 scripts for entering and exiting, it worked but not for long. Do i really need 2 scripts? and how to fix it

script 1

local ProximityPromt = script.Parent
local CountPlayer = workspace.Target.value
local Script2 = script.Parent.Script2
ProximityPromt.Triggered:Connect(function(Player)
	
	Player.Character.HumanoidRootPart.Position = game.Workspace.Target.Position
	CountPlayer.Value += 1
	ProximityPromt.ActionText = "Exit"
	Script2.Enabled = true
	script.Enabled = false
end)

script 2

local ProximityPromt = script.Parent
local CountPlayer = workspace.Target2.value
local Script1 = script.Parent.Script1
ProximityPromt.Triggered:Connect(function(Player)
	
	Player.Character.HumanoidRootPart.Position = game.Workspace.Target2.Position
	CountPlayer.Value -= 1
	ProximityPromt.ActionText = "Interact"
	Script1.Enabled = true
	script.Enabled = false
	
end)

a single script can handle both teleports as long as you can index both parts
the first thing you need to do is parent both parts to a model so the script knows what two parts are being used for teleporting
after that you need a script that can dynamically detect what part is being used:

local Parts = script.Parent:GetChildren()
for i,Part in Parts do
    Part:FindFirstChildOfClass("ProximityPrompt").Triggered:Connect(function(Player)
        if not Player.Character or not Player.Character:FindFirstChild("HumanoidRootPart") then return end
        Player.Character.HumanoidRootPart.CFrame = Parts[i+1 % (#Parts+1) + 1].CFrame
    end)
end

this will work if you place a proximity prompt in each part and have the script parented to the model.
this script works because it is getting all of the parts that are children of the model and connecting to their proximity prompt with a loop. I am using a math equation to determine the next part to teleport to (to avoid uneeded lines) that i will describe briefly. i is the index of the Parts table, and the % (modulo) operator returns the remainder of the division operation. Thus, I am able to determine when you are at the end or beggining of the table which then enabled me to loop back to the beggining of the table. This operation will only work on two table indexes though, and will result in unwanted behavior for 3+ teleporters.

1 Like

use :PivotTo() instead of .HumanoidRootPart.Position on the player’s character i.e.

playerWhoTriggered.Character:PivotTo(targetPosition)
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.