Cannot fix custom camera even during live testing (CFrame issue)

  1. I needed to make a part that when touched, disabled the rail system and goes back to roblox’s default camera system. When I am testing and delete it, I am still on the rail but the camera is frozen. I can still move around. I have another script that helps the camera tell it where the rail is and not to stray from it.

  2. The only thing is, I can’t. I have tried almost anything I can think of. Deleting the actual camera script before testing it seems to be the only solution, but I can’t do that as I need to disable the script at the very end.

  3. I have gotten so tired I decided to purge every single item while live testing. And it still did not work! Sometimes the camera is at 0,0,0 directly.

This is my camera script

local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local player = Players.LocalPlayer
local camera = workspace.CurrentCamera

local cameraRemote = ReplicatedStorage:WaitForChild("SetupCameraRail")

-- start with first rail
local currentRail = workspace:WaitForChild("Course1CameraRail")

-- function to calculate start & end points of the rail
local function getRailEnds(railPart)
	local startPoint = railPart.Position - railPart.CFrame.RightVector * (railPart.Size.X / 2)
	local endPoint = railPart.Position + railPart.CFrame.RightVector * (railPart.Size.X / 2)
	return startPoint, endPoint
end

local function clamp(val, min, max)
	if val < min then return min end
	if val > max then return max end
	return val
end

local function closestPointOnLineSegment(a, b, p)
	local ab = b - a
	local t = ((p - a):Dot(ab)) / ab.Magnitude^2
	t = clamp(t, 0, 1)
	return a + ab * t
end

-- fixed rotation of the camera (adjust as needed)
local fixedCameraCFrame = CFrame.Angles(0, math.rad(-90), 0)

-- listen for camera rail updates from server
cameraRemote.OnClientEvent:Connect(function(newRail)
	print("Received camera rail event!")
	currentRail = newRail

	-- immediately update camera position
	local character = player.Character
	local hrp = character and character:FindFirstChild("HumanoidRootPart")
	if hrp and currentRail then
		local railStart, railEnd = getRailEnds(currentRail)
		local playerPos = hrp.Position

		local cameraPosOnRail = closestPointOnLineSegment(railStart, railEnd, playerPos)

		local cameraPosition = Vector3.new(
			cameraPosOnRail.X,
			playerPos.Y + 5,
			cameraPosOnRail.Z
		)

		camera.CFrame = CFrame.new(cameraPosition) * fixedCameraCFrame
	end
end)

-- update camera every frame
RunService.RenderStepped:Connect(function()
	local character = player.Character
	local hrp = character and character:FindFirstChild("HumanoidRootPart")
	if hrp and currentRail then
		local playerPos = hrp.Position

		local railStart, railEnd = getRailEnds(currentRail)

		local cameraPosOnRail = closestPointOnLineSegment(railStart, railEnd, playerPos)

		local cameraPosition = Vector3.new(
			cameraPosOnRail.X,
			playerPos.Y + 5,
			cameraPosOnRail.Z
		)

		camera.CFrame = CFrame.new(cameraPosition) * fixedCameraCFrame
	end
end)

The red part is the “rail” the camera is stuck, as the player moves through the course, the camera stays in that part only, until they finish and teleport to the next course, which the camera moves to that courses rail. There was a trigger also inside the doorway that was meant to destroy all instances of both scripts either way. I have tried it without deleting it and it still didn’t work :sob:

Fixed! I found someone with the same problem here: Camera position stuck at around 0, 0, 0 (thank god this person figured it out because I wouldn’t have thought of deleting 6 letters to fix it :folded_hands:)