So, I’ve made a keybind that is supposed to turn of the camera angle script that was made by @vsnry and for some reason this doesn’t work. I’ve made the script to check if the camerascript is disabled, if it is then when the players press “P” the camera script is enabled. I’ve also added an “else” feature that checks if the camerascript is enabled and if it is then it turns of when “P” is pressed, here is the script to check it out for yourself.
Script:
> --Variables
local CameraScript = game.StarterPlayer.StarterPlayerScripts.Camerascript
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
local Humanoid = plr.Character:FindFirstChild('Humanoid')
--Script
mouse.KeyDown:Connect(function(Key)
if Key == "p" then
if CameraScript.Disabled == true then
CameraScript.Disabled = false
else
CameraScript.Disabled = true
end
end
end)
CameraScript:
-------------
--- INDEX ---
-------------
--Services
local userInputService = game:GetService("UserInputService")
local runService = game:GetService("RunService")
--Player & Character
local localPlayer = game:GetService("Players").LocalPlayer
--Other
local Camera = game:GetService("Workspace").CurrentCamera
--------------
--- VALUES ---
--------------
local xAngle = 0
local yAngle = 0
local cameraPos = Vector3.new(3.5,0,7.5)
----------------------
--- INITIALIZATION ---
----------------------
wait(1)
Camera.CameraType = Enum.CameraType.Scriptable
userInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
-----------------
--- FUNCTIONS ---
-----------------
userInputService.InputChanged:Connect(function(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.RenderStepped: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)
end
end)
After you disabled the CameraScript you have to reset the player’s camera.
We are using UserInputService since Mouse.KeyDown is deprecated.
local UserInputService = game:GetService("UserInputService")
local LocalPlayer = game.Players.LocalPlayer
function resetCamera() -- A function to reset the camera.
local Camera = workspace.CurrentCamera
Camera.CameraType = Enum.CameraType.Custom
Camera.CameraSubject = workspace:WaitForChild(game.Players.LocalPlayer.Name).Humanoid
Camera.CFrame = workspace:WaitForChild(game.Players.LocalPlayer.Name).Head.CFrame
UserInputService.MouseBehavior = Enum.MouseBehavior.Default
end
UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
if gameProcessedEvent then return end -- "Indicates whether the game engine internally observed this input and acted on it. Generally this refers to UI processing, so if a button was touched or clicked from this input, gameProcessedEvent would be true."
if input.KeyCode == Enum.KeyCode.P then --input is an object which contains a lot of useful information such as Delta, KeyCode, Position, UserInputState etc.. (https://developer.roblox.com/en-us/api-reference/class/InputObject)
if not LOCALSCRIPTLOCATION.Disabled then
LOCALSCRIPTLOCATION.Disabled = true
resetCamera()
else
LOCALSCRIPTLOCATION.Disabled = false
end
end
end)
It’ll take at least one second for the camera to be enabled again since there’s a wait in the initialization.
Yes, you should put it in a separate LocalScript. LOCALSCRIPTLOCATION should be changed with the Camera’s LocalScript Location.
For example:
local UserInputService = game:GetService("UserInputService")
local LocalPlayer = game.Players.LocalPlayer
function resetCamera()
local Camera = workspace.CurrentCamera
Camera.CameraType = Enum.CameraType.Custom
Camera.CameraSubject = workspace:WaitForChild(game.Players.LocalPlayer.Name).Humanoid
Camera.CFrame = workspace:WaitForChild(game.Players.LocalPlayer.Name).Head.CFrame
UserInputService.MouseBehavior = Enum.MouseBehavior.Default
end
UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
if gameProcessedEvent then return end
if input.KeyCode == Enum.KeyCode.P then
if LocalPlayer.Character:FindFirstChild("Camera") == nil then return end
if not LocalPlayer.Character:FindFirstChild("Camera").Disabled then
LocalPlayer.Character:FindFirstChild("Camera").Disabled = true
resetCamera()
else
LocalPlayer.Character:FindFirstChild("Camera").Disabled = false
end
end
end)
The LocalScript above should be put in StarterPlayerScripts, while the Camera LocalScript should be put in StarterCharacterScripts.
It takes a second because there’s a wait in the camera’s initialization LocalScript.
I’ve made a place for you with everything ready. Tell me if you experience the same issue. CameraScript.rbxl (20.1 KB)
If you plan to have it disabled when the player joins, you can simply remove the wait() from the initialization.
--- INDEX ---
-------------
--Services
local userInputService = game:GetService("UserInputService")
local runService = game:GetService("RunService")
--Player & Character
local localPlayer = game:GetService("Players").LocalPlayer
--Other
local Camera = game:GetService("Workspace").CurrentCamera
--------------
--- VALUES ---
--------------
local xAngle = 0
local yAngle = 0
local cameraPos = Vector3.new(3.5,0,7.5)
----------------------
--- INITIALIZATION ---
----------------------
Camera.CameraType = Enum.CameraType.Scriptable
userInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
-----------------
--- FUNCTIONS ---
-----------------
userInputService.InputChanged:Connect(function(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.RenderStepped: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)
end
end)
Same issue, though I have enabled the camera script, since I want my game to start of with that, but the only problem is the waiting. It works the same it’s just the camera script is enabled in my place.