hi so my main camera script was working before its supposed to attach back to the character when they respawn but every since i made my main menu for my game it is just not working when i die and respawn and its honestly super fustrating i dont understand ive tried almost every solution i could think of and still nothing please im actually begging for this script to not be a burden anymore i hate dealing with this script.
heres the current version of the script
local RunService = game:GetService("RunService")
local InputService = game:GetService("UserInputService")
local cameraUpdateConnection = nil
local mouseMovedConnection = nil
local characterAddedConnection = nil
local characterRemovingConnection = nil
local uniqueId = "UpdateCamera_" .. tostring(tick())
-- Function to print debug messages
local function debugPrint(message)
print("[CameraScript] " .. message)
end
-- Function to shut down the camera script
local function shutdownCamera()
debugPrint("Shutting down camera script.")
if mouseMovedConnection then
mouseMovedConnection:Disconnect()
mouseMovedConnection = nil
end
if cameraUpdateConnection then
RunService:UnbindFromRenderStep(cameraUpdateConnection)
cameraUpdateConnection = nil
end
end
-- Function to initialize the camera
local function initializeCamera(character)
debugPrint("Initializing camera...")
wait(0.2) -- Wait to ensure everything is properly loaded
local Camera = game.Workspace.CurrentCamera
local Player = game.Players.LocalPlayer
character = character or Player.Character or Player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid", 10)
if not humanoid then
debugPrint("Failed to find Humanoid in the new character.")
return
end
local Head = character:WaitForChild("Head", 10)
local Torso = character:WaitForChild("Torso", 10)
local RootPart = character:WaitForChild("HumanoidRootPart", 10)
local Neck = Torso:WaitForChild("Neck", 10)
-- Ensure all parts are available before proceeding
if not Head or not Torso or not RootPart or not Neck then
debugPrint("Failed to find essential parts in the new character.")
return
end
-- Set initial camera properties
Camera.FieldOfView = 90
Camera.CameraType = Enum.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 = Enum.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.CFrame = PreCam * CFrame.new(0, 0, Zoom)
end
-- Ensure we unbind any previous camera update connection before binding a new one
if cameraUpdateConnection then
RunService:UnbindFromRenderStep(cameraUpdateConnection)
cameraUpdateConnection = nil
end
uniqueId = "UpdateCamera_" .. tostring(tick()) -- Generate a new unique identifier
RunService:BindToRenderStep(uniqueId, Enum.RenderPriority.Camera.Value, CameraUpdate)
cameraUpdateConnection = uniqueId
local function OnFocusGained()
debugPrint("Window focused")
if mouseMovedConnection then
mouseMovedConnection:Disconnect()
end
mouseMovedConnection = InputService.InputChanged:Connect(MouseMoved)
InputService.MouseBehavior = Enum.MouseBehavior.LockCenter
end
local function OnFocusLost()
debugPrint("Window focus lost")
if mouseMovedConnection then
mouseMovedConnection:Disconnect()
end
InputService.MouseBehavior = Enum.MouseBehavior.Default
end
InputService.WindowFocused:Connect(OnFocusGained)
InputService.WindowFocusReleased:Connect(OnFocusLost)
_G.setCameraShakeEffect = function(effectFunction)
cameraShakeEffect = effectFunction
end
_G.setJumpScareActive = function(active)
isJumpScareActive = active
end
end
-- Function to clean up connections
local function cleanUp()
shutdownCamera()
if characterAddedConnection then
characterAddedConnection:Disconnect()
characterAddedConnection = nil
end
if characterRemovingConnection then
characterRemovingConnection:Disconnect()
characterRemovingConnection = nil
end
debugPrint("Cleaned up connections.")
end
-- Function to handle character addition
local function onCharacterAdded(character)
debugPrint("Character added.")
cleanUp()
initializeCamera(character)
end
-- Function to handle character removal
local function onCharacterRemoving()
debugPrint("Character removing.")
shutdownCamera()
end
-- Connect the initializeCamera function to the CharacterAdded event
local Player = game.Players.LocalPlayer
characterAddedConnection = Player.CharacterAdded:Connect(onCharacterAdded)
characterRemovingConnection = Player.CharacterRemoving:Connect(onCharacterRemoving)
-- Function to start the camera script when the menu is done
function startCamera()
debugPrint("Starting camera script...")
cleanUp()
initializeCamera()
end
-- Expose the function to be called from the main menu script
_G.startCamera = startCamera
-- Ensure the camera script doesn't run until startCamera is called
debugPrint("Camera script loaded and waiting for start signal.")
-- Reapply the camera settings after death
Player.CharacterAdded:Connect(function(character)
wait(0.2)
debugPrint("Reapplying camera settings after respawn.")
startCamera()
end)