How to make teleport script let character keep camera position?

So, I was messing around making a teleport script and I settled on something I liked that had this weird effect, but the issue with it is that whenever the whole animation thingymajig is finished the camera orientation isn’t kept. Anyone got a clue on how to fix that?


Teleport LocalScript:

local inputService = game:GetService("UserInputService")
local tweenService = game:GetService("TweenService")
local repStorage = game:GetService("ReplicatedStorage")
local mouse = game.Players.LocalPlayer:GetMouse()
local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local humrp = char:WaitForChild("HumanoidRootPart")
local charheight = Vector3.new(0,char:GetExtentsSize().Y + 1, 0)

inputService.InputBegan:Connect(function(inp, gpe) 
	if gpe == false then

		if inputService:IsKeyDown(Enum.KeyCode.T) then
			
			if mouse.Target and char.Humanoid.FloorMaterial ~= Enum.Material.Air then
				
				local teleportPos = mouse.Hit - charheight
				local portalTween = tweenService:Create(humrp,
					TweenInfo.new(0.4, Enum.EasingStyle.Linear),
					{CFrame = teleportPos + charheight*2})

				for _, v in pairs(char:GetChildren()) do
					if v:isA("Part") then
						v.CollisionGroup = "Noclip"
					end
				end
				task.wait(0.35)
				humrp.Anchored = true
				
				char:PivotTo(CFrame.new(0,0,0,0,0,0,0) * teleportPos)
				print(CFrame.new(0,0,0,0,0,0,0) * teleportPos)
				
				portalTween:Play()
				portalTween.Completed:Wait()
				
				for _, v in pairs(char:GetChildren()) do
					if v:isA("Part") then
						v.CollisionGroup = "Default"
					end
				end
				
				humrp.Anchored = false
				char.Humanoid:ChangeState(Enum.HumanoidStateType.GettingUp)

			end
		end
	end
end)


Video Showcasing Issue:




On a sidenote, anybody got a clue what causes that weird strobe effect? I’m assuming it’s because the inputted teleport position is null (or atleast I’m assuming it is null), but the HMRP never says the CFrame is set to null or NAN.

you should get the direction of the cam to the player first before tp. Then add it to the camera position after the tp. Like this:

local camDirection = camera.CFrame.Position - character.Head.Position
tp()
camera.CFrame=CFrame.new(camera.CFrame.Position+camDirection)*camera.CFrame.Rotation

I might be wrong because I dont have studio to test it out rn

1 Like

Thanks. That sort of worked, I had to change camDirection to just be camera.CFrame though.

In the end it ended up looking like this:

local camDirection = camera.CFrame
tp()
camera.CFrame = camDirection
2 Likes

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