CFrame LookVector Weirdness

I’m trying to set up a two-way teleporter. The desired outcome is that when you enter one portal, you spawn 4 studs in front of the other portal. This is my code:

local players = game:GetService("Players")

local canTP = true

local function onTouch(t, dest:Part)
	if not canTP then return end
	if players:GetPlayerFromCharacter(t.Parent) == players.LocalPlayer then
		canTP = false
		-- teleport the player to 4 studs in front of the destination part (plus a 0.5 stud vertical boost so you aren't in the ground)
		t.Parent.PrimaryPart.CFrame = dest.CFrame * CFrame.new(dest.CFrame.LookVector.X * 4, 0.5, dest.CFrame.LookVector.Z * 4)
		task.delay(1, function()
			canTP = true
		end)
	end
end

-- oh yea this is in a modulescript btw
return function()
	-- the explorer layout is as follows:
	-- the script is in a folder containing folders with two portal models in each one
	-- both portal models have the "Portal" part in them
	for _, v in pairs(script.Parent:GetChildren()) do
		if v:IsA("Folder") then
			local a = v:WaitForChild("A")
			local b = v:WaitForChild("B")
			local ap:BasePart = a:WaitForChild("Portal")
			local bp:BasePart = b:WaitForChild("Portal")
			
			ap.Touched:Connect(function(t)
				onTouch(t, bp)
			end)
			bp.Touched:Connect(function(t)
				onTouch(t, ap)
			end)
		end
	end
end

As you can see in the onTouch function, I use CFrame.LookVector to calculate the front facing side of the portals.

This… almost works. It does indeed teleport you to the other portal, but in a really weird way.
Video Link (hosted on streamable)
As you can see, something is off with the “front of the portal” calculation. It just seems to pick some random side and teleport you there instead. Even weirder is that it’s different for both portals! (Notice how one teleports you to the side of it and one teleports you behind it??)
And yes, the glowing sides of each portal are indeed the Front surface faces of each portal part.

Any idea what could be happening and how I’d go about fixing it?

try this

t.parent.PrimaryPart.CFrame = dest.CFrame + dest.CFrame.LookVector * 4 + CFrame.new(0, 0.5, 0)

This mixes adding Vector3 and CFrame values which results in an error.

my bad gang

t.parent.PrimaryPart.CFrame = dest.CFrame + dest.CFrame.LookVector * 4 + Vector3.new(0, 0.5, 0)

this one actually works i tested it

Huh. I’m honestly not sure why this works and mine doesn’t, but this does indeed fix the issue.
Thanks!

you are multiplying the cframe by the lookvector instead of adding it

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