Keybind used to disable camera script doesn't work

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)

Screenshots:
image
image
image

2 Likes

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.

---------------------- 

--- INITIALIZATION --- 

---------------------- 

wait(1)

Do I put this in a separate local script? And what exactly do you mean by localscriptlocation?

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.

1 Like

Doesn’t work…
(30 characters)

Do you get any errors? Where are your scripts placed? (Can I see a screenshot with your explorer?) Make sure you changed:

LocalPlayer.Character:FindFirstChild("Camera")

with:

LocalPlayer.Character:FindFirstChild("The LocalScript Name")

Alright, I have a question
(30 chars)

1 Like

Do I have to keep image
or do I change the location?

You should keep it. That’s the camera’s location. It’s the same, no matter where the LocalScript is placed.

Umm… It just rotates my camera when I press P.
It doesn’t enable the Camerascript. I will try to edit the script

May I see a screenshot of your explorer and where you placed each LocalScript?

imaimage|204x36 ge
image

Actually, if I have it enabled (I am talking about the camerascript) that works when I press P, but when I press P again it just rotates my camera.

Wait, it works but takes a second or two. I have to leave the camera script enabled.

Is there a way to make it work faster?

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.

I have an Idea. I will add a please wait gui whenever they press P. Thank you anyway!
Sorry for wasting your time. Have a great day/night!

1 Like