So I have a mouse lock script that’s supposed to be like shift lock. The script works almost perfectly the only problem is when I die and change the CameraType, the Camera stays in the position it was when the player died. I figured the solution was to move the script to StarterCharacterScripts since it was originally in StarterPlayerScripts, but when I move it to StarterCharacterScripts it just doesn’t work at all.
Mouse lock video:
Camera after you die:
Script that changes CameraType:
userInputService.MouseBehavior = Enum.MouseBehavior.Default
Camera.CameraType = Enum.CameraType.Custom
Mouse lock script:
--Services
local userInputService = game:GetService("UserInputService")
local runService = game:GetService("RunService")
local RepStor = game:GetService("ReplicatedStorage")
--Player & Character
local localPlayer = game:GetService("Players").LocalPlayer
--Other
local Camera = game:GetService("Workspace").CurrentCamera
local Mouse = localPlayer:GetMouse()
--------------
--- VALUES ---
--------------
local xAngle = 0
local yAngle = 0
local cameraPos = Vector3.new(2,0,10)
local toggled = true
local input2
--[[-----------------------------------------------------------------------------------------
T O G G L E M A I N S C R I P T
--------------------------------------------------------------------------------------------]]
local function FocusToggle()
wait(1)
if toggled == true then
Camera.CameraType = Enum.CameraType.Scriptable
userInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
userInputService.InputChanged:Connect(function(input)
input2 = input
if input.UserInputType == Enum.UserInputType.MouseMovement then
xAngle = xAngle-input.Delta.x*0.4
--Clamp the vertical axis so it doesn't go upside down or glitch.
yAngle = math.clamp(yAngle-input.Delta.y*0.4,-80,80)
end
end)
runService.Heartbeat:Connect(function()
local Character = localPlayer.Character
local rootPart = Character:FindFirstChild("HumanoidRootPart")
if Character and rootPart then
--Set rotated start cframe inside head
local startCFrame = CFrame.new((rootPart.CFrame.p + Vector3.new(0,2,0)))*CFrame.Angles(0, math.rad(xAngle), 0)*CFrame.Angles(math.rad(yAngle), 0, 0)
--Set camera focus and cframe
local cameraCFrame = startCFrame + startCFrame:vectorToWorldSpace(Vector3.new(cameraPos.X,cameraPos.Y,cameraPos.Z))
local cameraFocus = startCFrame + startCFrame:vectorToWorldSpace(Vector3.new(cameraPos.X,cameraPos.Y,-50000))
Camera.CFrame = CFrame.new(cameraCFrame.p,cameraFocus.p)
---------------------------[C A M E R A C O L L I S I O N]----------------------------------
if Character:FindFirstChild("Head") then
local Camera = game.Workspace.CurrentCamera
local CameraRay = Ray.new(Character.Head.Position, Camera.CFrame.Position - Character.Head.Position)
local Ignore = {Character}
local HitPart, HitPosition = game.Workspace:FindPartOnRayWithIgnoreList(CameraRay, Ignore)
if HitPart ~= nil and HitPart:IsA("BasePart") and HitPart.CanCollide == true and HitPart.Transparency <1 then
Camera.CFrame = (Camera.CFrame - (Camera.CFrame.Position - HitPosition)) + (Character.Head.Position - Camera.CFrame.Position).Unit
end
end
--------------------------------------------------------------------------------------------
end
end)
---------------------------------[S C R O L L I N G]-----------------------------------------------
Mouse.WheelForward:Connect(function()
if cameraPos.Z == 4 then return end
cameraPos = Vector3.new(cameraPos.X, cameraPos.Y, cameraPos.Z - 2)
end)
Mouse.WheelBackward:Connect(function()
if cameraPos.Z == 24 then return end
cameraPos = Vector3.new(cameraPos.X, cameraPos.Y, cameraPos.Z + 2)
end)
end
end
---------------------------------------------------------------------------------------------
localPlayer.CharacterAdded:Connect(function(char)
if userInputService.KeyboardEnabled == false and userInputService.TouchEnabled == true then --If there is no keyboard but a touch device
return end
FocusToggle()
end)
localPlayer.CharacterRemoving:Connect(function(char)
toggled = false
end)
Reading the mouse lock script may not be necessary, I just need to know how to fix the camera after the player dies.