Camera script being ignored after 1 death

i have a camera script that worked after death multiple times but now its not because of my main menu script enabling it.
heres the camera script

local module = {}

local initialized = false
local cameraUpdateConnection = nil
local mouseMovedConnection = nil
local characterAddedConnection = nil

local function initializeCamera()
	wait(0)

	local InputService = game:GetService("UserInputService")
	local RunService = game:GetService("RunService")
	local Camera = game.Workspace.CurrentCamera
	local Player = game.Players.LocalPlayer
	local Character = Player.Character or Player.CharacterAdded:Wait()
	local Head = Character:WaitForChild("Head")
	local Torso = Character:WaitForChild("Torso")
	local RootPart = Character:WaitForChild("HumanoidRootPart")
	local RootJoint = RootPart.RootJoint
	local Neck = Torso.Neck

	Camera.FieldOfView = 90
	Camera.CameraType = "Scriptable"
	InputService.MouseBehavior = Enum.MouseBehavior.LockCenter

	local v3 = Vector3.new
	local cf = CFrame.new
	local inverse = cf().inverse
	local fromAxisAngle = CFrame.fromAxisAngle
	local atan, atan2 = math.atan, math.atan2
	local acos = math.acos

	local function toAxisAngleFromVector(v)
		local z = v.z
		return z*z < 0.99999 and v3(v.y, -v.x, 0).unit * acos(-z) or v3()
	end

	local function AxisAngleLookOrientation(c, v, t)
		local c = c - c.p
		local rv = (inverse(c) * v).unit
		local rz = rv.z
		return rz*rz < 0.99999 and c * fromAxisAngle(v3(rv.y, -rv.x, 0), acos(-rz) * (t or 1)) or c
	end

	local function AxisAngleLookNew(v, t)
		local rv = v.unit
		local rz = rv.z
		return rz*rz < 0.99999 and fromAxisAngle(v3(rv.y, -rv.x, 0), acos(-rz) * (t or 1)) or cf()
	end

	local function AxisAngleLook(c, v, t)
		local rv = (inverse(c) * v).unit
		local rz = rv.z
		return rz*rz < 0.99999 and c * fromAxisAngle(v3(rv.y, -rv.x, 0), acos(-rz) * (t or 1)) or c
	end

	local Sensitivity = 0.005
	local CameraDirection = Vector3.new(0, 0, 1)

	local function EulerAnglesYX(l)
		local x, z = l.x, l.z
		return atan(l.y / (x*x + z*z)^0.5), -atan2(l.x, -z)
	end

	local function AnglesXY(l)
		local z = l.z
		return atan2(l.y, -z), -atan2(l.x, -z)
	end

	local function MouseMoved(Input)
		if Input.UserInputType == Enum.UserInputType.MouseMovement then
			local dx, dy = Input.Delta.x * Sensitivity, Input.Delta.y * Sensitivity
			local m2 = dx*dx + dy*dy
			if m2 > 0 then
				CameraDirection = (AxisAngleLookOrientation(RootPart.CFrame, CameraDirection) * fromAxisAngle(v3(-dy, -dx, 0), m2^0.5)).lookVector
			end
			local RootOrientation = RootPart.CFrame - RootPart.Position
			local RelativeDirection = RootOrientation:inverse() * CameraDirection
			local AngX, AngY = AnglesXY(RelativeDirection)
			if AngX < -1.57 * 11 / 12 then
				local y, z, c, s = RelativeDirection.y, RelativeDirection.z, math.cos(-1.57 * 11 / 12 - AngX), -math.sin(-1.57 * 11 / 12 - AngX)
				z, y = z * c - y * s, z * s + y * c
				CameraDirection = RootOrientation * v3(RelativeDirection.x < 0 and -(1 - y*y - z*z)^0.5 or (1 - y*y - z*z)^0.5, y, z)
			elseif AngX > 1.57 * 11 / 12 then
				local y, z, c, s = RelativeDirection.y, RelativeDirection.z, math.cos(1.57 * 11 / 12 - AngX), -math.sin(1.57 * 11 / 12 - AngX)
				z, y = z * c - y * s, z * s + y * c
				CameraDirection = RootOrientation * v3(RelativeDirection.x < 0 and -(1 - y*y - z*z)^0.5 or (1 - y*y - z*z)^0.5, y, z)
			end
		end
	end

	mouseMovedConnection = InputService.InputChanged:Connect(MouseMoved)

	Neck.C1 = cf()

	local _
	local DirectionBound = 3.14159 / 3
	local CurrentAngY = 0

	local cameraShakeEffect = nil
	local isJumpScareActive = false

	local function CameraUpdate(deltaTime)
		if _G.isJumpScareActive then
			return
		end

		if cameraShakeEffect then
			cameraShakeEffect(deltaTime)
		end

		Camera.CameraType = "Scriptable"
		local cx, cz = CameraDirection.x, CameraDirection.z
		local rvx, rvz = RootPart.Velocity.x, RootPart.Velocity.z
		if rvx*rvx + rvz*rvz > 4 and cx*rvx + cz*rvz < -0.5 * (cx*cx + cz*cz)^0.5 * (rvx*rvx + rvz*rvz)^0.5 then
			DirectionBound = math.min(DirectionBound * 0.9, math.abs(CurrentAngY * 0.9))
		else
			DirectionBound = DirectionBound * 0.1 + 3.14159 / 3 * 0.9
		end
		local AngX, AngY = EulerAnglesYX((RootPart.CFrame - RootPart.Position):inverse() * CameraDirection)
		if AngY > DirectionBound then
			RootPart.CFrame = RootPart.CFrame * CFrame.Angles(0, AngY - DirectionBound, 0)
		elseif AngY < -DirectionBound then
			RootPart.CFrame = RootPart.CFrame * CFrame.Angles(0, AngY + DirectionBound, 0)
		end
		_, CurrentAngY = EulerAnglesYX((RootPart.CFrame - RootPart.Position):inverse() * CameraDirection)
		local CameraOrientation = AxisAngleLookNew((RootPart.CFrame - RootPart.Position):inverse() * CameraDirection, 1)
		Neck.C0 = CFrame.new(0, 1, 0) * CameraOrientation * CFrame.new(0, 0.5, 0)

		local PreCam = AxisAngleLook(RootPart.CFrame * cf(0, 1, 0), RootPart.CFrame * v3(0, 1, 0) + CameraDirection) * CFrame.new(0, 0.825, 0)
		local Zoom = -0.5
		Camera.CoordinateFrame = PreCam * CFrame.new(0, 0, Zoom)
	end

	cameraUpdateConnection = RunService.RenderStepped:Connect(CameraUpdate)

	local function OnFocusGained()
		if mouseMovedConnection.Connected then
			mouseMovedConnection:Disconnect()
		end
		mouseMovedConnection = InputService.InputChanged:Connect(MouseMoved)
		InputService.MouseBehavior = Enum.MouseBehavior.LockCenter
	end

	InputService.WindowFocused:Connect(OnFocusGained)

	_G.setCameraShakeEffect = function(effectFunction)
		cameraShakeEffect = effectFunction
	end

	_G.setJumpScareActive = function(active)
		isJumpScareActive = active
	end
end

local function cleanUp()
	if mouseMovedConnection then
		mouseMovedConnection:Disconnect()
		mouseMovedConnection = nil
	end
	if cameraUpdateConnection then
		cameraUpdateConnection:Disconnect()
		cameraUpdateConnection = nil
	end
end

local function onCharacterAdded(character)
	cleanUp()
	initializeCamera()
end

-- Connect the initializeCamera function to the CharacterAdded event
local Player = game.Players.LocalPlayer
characterAddedConnection = Player.CharacterAdded:Connect(onCharacterAdded)

-- Initialize the camera for the first character
if Player.Character then
	initializeCamera()
end

-- Clean up when the script is disabled
module.onDisable = function()
	cleanUp()
	if characterAddedConnection then
		characterAddedConnection:Disconnect()
		characterAddedConnection = nil
	end
end

-- Re-initialize when the script is enabled
module.onEnable = function()
	characterAddedConnection = Player.CharacterAdded:Connect(onCharacterAdded)
	if Player.Character then
		initializeCamera()
	end
end

return module

and heres my main menu script that enables the scripts

task.wait()
local TweenService = game:GetService("TweenService")

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local mouse = player:GetMouse()

local currentCamera = workspace.CurrentCamera

local gui = script.Parent

local loadingframe = gui.Loading
local title = gui.Title
local play = gui.Play
local setting = gui.Settings
local credits = gui.Credits
local frames = gui.Frames

local folder = workspace.MenuScreen

local inMenu = true

-- Create a BoolValue to track if the GUI has been shown
local guiShownValue = player:FindFirstChild("GuiShown")
if not guiShownValue then
	guiShownValue = Instance.new("BoolValue")
	guiShownValue.Name = "GuiShown"
	guiShownValue.Parent = player
end

-- Create a BoolValue to track if the scripts have been enabled
local scriptsEnabledValue = player:FindFirstChild("ScriptsEnabled")
if not scriptsEnabledValue then
	scriptsEnabledValue = Instance.new("BoolValue")
	scriptsEnabledValue.Name = "ScriptsEnabled"
	scriptsEnabledValue.Parent = player
end

-- Function to handle initial GUI setup
local function setupInitialGUI()
	character.HumanoidRootPart.Anchored = true

	folder:WaitForChild("CameraPart")

	repeat wait()
		currentCamera.CameraType = Enum.CameraType.Scriptable
		currentCamera.CFrame = folder.CameraPart.CFrame
	until currentCamera.CFrame == folder.CameraPart.CFrame

	for _, v in pairs(loadingframe.Frame:GetChildren()) do
		if v:IsA("TextLabel") then
			v:Destroy()
		end
	end

	local maxTilt = 10
	game:GetService("RunService").RenderStepped:Connect(function()
		if inMenu == true then
			workspace.CurrentCamera.CFrame = folder.CameraPart.CFrame * CFrame.Angles(
				math.rad((((mouse.Y - mouse.ViewSizeY / 2) / mouse.ViewSizeY)) * -maxTilt),
				math.rad((((mouse.X - mouse.ViewSizeX / 2) / mouse.ViewSizeX)) * -maxTilt),
				0
			)
		end
	end)

	title.TextLabel.Text = folder.Settings.GameName.Value
	loadingframe.GameIcon.Image = "rbxassetid://"..folder.Settings.GameIcon.Value

	if not guiShownValue.Value then
		script.Parent.Loading.Visible = true

		repeat wait()
		until loadingframe.GameIcon.IsLoaded == true

		task.wait(1)

		TweenService:Create(loadingframe.GameIcon, TweenInfo.new(1), {ImageTransparency = 1}):Play()

		task.wait(1)

		for i = 1, 10 do
			local cmd = script.TextLabel:Clone()
			cmd.Parent = loadingframe.Frame

			if i == 1 then
				cmd.Text = "> welcome to "..folder.Settings.GameName.Value

			elseif i == 2 then
				cmd.Text = "> " ..player.Name.." joined"

			elseif i == 3 then
				cmd.Text = "> getting player data... 0/"..#player:GetChildren()

				for v = 1, #player:GetChildren() do
					cmd.Text = "> getting player data... "..v.."/"..#player:GetChildren()
					task.wait(.025)
				end

			elseif i == 4 then
				cmd.Text = "> loading assets... 0/"..#workspace:GetChildren()

				for v = 1, #workspace:GetChildren() do
					cmd.Text = "> loading assets... "..v.."/"..#workspace:GetChildren()
					task.wait(.025)
				end

			elseif i == 5 then
				cmd.Text = "> importing files 0/"..#game.ReplicatedStorage:GetChildren()

				for v = 1, #game.ReplicatedStorage:GetChildren() do
					cmd.Text = "> importing files... "..v.."/"..#game.ReplicatedStorage:GetChildren()
					task.wait(.025)
				end

			elseif i == 6 then
				cmd.Text = "> running system.exe."
				task.wait(.2)
				cmd.Text = "> running system.exe.."
				task.wait(.2)
				cmd.Text = "> running system.exe..."

			elseif i == 7 then
				cmd.Text = "> booting up the game."
				task.wait(.2)
				cmd.Text = "> booting up the game.."
				task.wait(.2)
				cmd.Text = "> booting up the game..."

			elseif i == 8 then
				cmd.Text = ""

			elseif i == 9 then
				cmd.Text = "> gamename:\\playername>Boot Success!"

			elseif i == 10 then
				cmd.Text = "> entering the game"
			end

			task.wait(.5)
		end

		loadingframe.Frame.Visible = false

		task.wait(1)

		currentCamera.FieldOfView = 40
		TweenService:Create(currentCamera, TweenInfo.new(.8, Enum.EasingStyle.Back), {FieldOfView = 70}):Play()
		TweenService:Create(loadingframe, TweenInfo.new(.3), {Size = UDim2.new(0,0, 0,0)}):Play()
		guiShownValue.Value = true
	end
end

-- Function to handle enabling scripts
local function enableScripts()
	if not scriptsEnabledValue.Value then
		-- Enable the required scripts
		local hidePlayerMouse = player:WaitForChild("PlayerGui"):WaitForChild("HidePlayerMouse")
		local mainCamera = player:WaitForChild("PlayerGui"):WaitForChild("MainCamera")
		local localTeleportScript = player:WaitForChild("PlayerScripts"):WaitForChild("LocalTeleportScript")

		hidePlayerMouse.Enabled = true
		mainCamera.Enabled = true
		localTeleportScript.Enabled = true

		scriptsEnabledValue.Value = true
	end
end

-- Run the initial GUI setup if it hasn't been shown before
if not guiShownValue.Value then
	setupInitialGUI()
end

play.Frame.Title.Activated:Connect(function()
	TweenService:Create(loadingframe, TweenInfo.new(.3), {Size = UDim2.new(1,0, 1,0)}):Play()
	TweenService:Create(currentCamera, TweenInfo.new(.8, Enum.EasingStyle.Back), {FieldOfView = 40}):Play()

	task.wait(1)

	inMenu = false
	currentCamera.CameraType = Enum.CameraType.Custom

	character.HumanoidRootPart.Anchored = false

	if workspace:FindFirstChildOfClass("SpawnLocation") then
		character.HumanoidRootPart.CFrame = workspace:FindFirstChildOfClass("SpawnLocation").CFrame * CFrame.new(0, 2, 0)
	else
		character.HumanoidRootPart.CFrame = CFrame.new(0, 2, 0)
	end

	task.wait(1)

	TweenService:Create(currentCamera, TweenInfo.new(.8, Enum.EasingStyle.Back), {FieldOfView = 70}):Play()
	TweenService:Create(loadingframe, TweenInfo.new(.3), {Size = UDim2.new(0,0, 0,0)}):Play()

	task.wait(1)

	gui.Enabled = false

	enableScripts()
end)

-- Ensure scripts are enabled on character respawn
character:WaitForChild("Humanoid").Died:Connect(function()
	character = player.CharacterAdded:Wait()
	character.HumanoidRootPart.Anchored = false
	enableScripts()
end)

setting.Frame.Title.Activated:Connect(function()
	frames.Credits.Visible = false
	frames.Settings.Visible = not frames.Settings.Visible
end)

credits.Frame.Title.Activated:Connect(function()
	frames.Settings.Visible = false
	frames.Credits.Visible = not frames.Credits.Visible
end)
2 Likes

can someone help cuz im honestly stuck and would really appreciate help

You need to require your camera module for it to work. Modules also don’t have an Enabled bool

im not following what you mean could you provide me an example possibly, sorry

in your enableScripts() function, you should enable the camera script, as in:

local mainCamera = require(player:WaitForChild("PlayerGui"):WaitForChild("MainCamera"))

althought your camera module is completely clientside so you have to disable/enable it on the local user’s end, not on the server’s

ah i see now well after acknowledging and changing it kinda just gives me errors

15:11:13.845 Attempted to call require with invalid argument(s). - Client - Client:157

hmm, maybe instead of that you should require after the camera script is confirmed to exist?

local mainCamera = player:WaitForChild("PlayerGui"):WaitForChild("MainCamera")
require(mainCamera)

if you would like i can message you the 2 scripts

sure, I’ll help with what I can

alrighty i really appreciate the help!

where is this script located? if not in starterscripts then it wont work

camera is in startergui same as menu