Make player camera go to normal when E is pressed

How would I make it so once the player presses E the camera is set back to normal? I’m trying to make a shop that makes the character go to a custom camera, but once the player presses E, I’m wondering how I could get that to work.

1 Like

You just need to set the CurrentCamera to Custom

Something like that

game.Workspace.CurrentCamera.CameraType = Enum.CameraType.Custom

The Camera should go back to normal.

I tried doing that before and nothing happens, I set the camera to a part in workspace.
(my code:)

local plr = game:GetService("Players").LocalPlayer
local Character = plr.Character or plr.CharacterAdded:Wait()
local CameraInst = workspace.Camera
local replicatedst = game:GetService("ReplicatedStorage")
local CameraPart = workspace.CameraPart.CFrame
local mouse = game:GetService("Players").LocalPlayer:GetMouse()
local currentcam = game.Workspace.CurrentCamera

replicatedst.Camera.OnClientEvent:Connect(function()
	CameraInst.CameraType = Enum.CameraType.Scriptable
	CameraInst.CFrame = CameraPart
	--// Move cam
	local maxTilt = 10
	game:GetService("RunService").RenderStepped:Connect(function()
		CameraInst.CFrame = CameraPart * 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)


local UIS = game:GetService("UserInputService")
local KeyBind = "R"
UIS.InputBegan:Connect(function(input,Chatting)
	local camera = workspace.CurrentCamera
	if input.KeyCode == Enum.KeyCode.R then
		game.Workspace.CurrentCamera.CameraType = Enum.CameraType.Custom
	end
end)

The camera does change to custom, (I checked it)

Instead of

CameraInst.CameraType = Enum.CameraType.Scriptable

try

currentcam.CameraType = Enum.CameraType.Scriptable

added that, same issue.
I tried printing something and it does print. I’m not sure why it isn’t working

This will work.

local plr = game:GetService("Players").LocalPlayer
local Character = plr.Character or plr.CharacterAdded:Wait()
local CameraInst = workspace.Camera
local replicatedst = game:GetService("ReplicatedStorage")
local CameraPart = workspace.CameraPart.CFrame
local mouse = game:GetService("Players").LocalPlayer:GetMouse()
local currentcam = game.Workspace.CurrentCamera

replicatedst.Camera.OnClientEvent:Connect(function()
	CameraInst.CameraType = Enum.CameraType.Scriptable
	CameraInst.CFrame = CameraPart
	--// Move cam
	local maxTilt = 10
	game:GetService("RunService").RenderStepped:Connect(function()
		CameraInst.CFrame = CameraPart * 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)


local UIS = game:GetService("UserInputService")
local KeyBind = "R"
UIS.InputBegan:Connect(function(input,Chatting)
	local camera = workspace.CurrentCamera
	if input.KeyCode == Enum.KeyCode.R then
		game.Workspace.CurrentCamera.CameraType = Enum.CameraType.Track -- Instead of 'Custom'
	end
end)

just to confirm, are you aware that the post says you want the camera to be changed when you press E, despite the code waiting for the user to press R?

input.KeyCode == Enum.KeyCode.R

i’m not sure if you changed plans mid-way through writing this post and sending the script, and it might not even lead to much as you say the camera does change to custom regardless.

also, you’re creating the following variable without it ever being used in the function:

local KeyBind = "R"

this could lead to nothing, but just want to understand everything prior to scrambling and messing around with the script.

it used to be e, I changed it to r a little while ago to make sure it wasn’t the key itself (I changed it back). Also the variable was being used previously but I changed the part it was being used in and forgot to remove it.

Yo try this

local plr = game:GetService("Players").LocalPlayer
local Character = plr.Character or plr.CharacterAdded:Wait()
local CameraInst = workspace.Camera
local replicatedst = game:GetService("ReplicatedStorage")
local CameraPart = workspace.CameraPart.CFrame
local mouse = game:GetService("Players").LocalPlayer:GetMouse()
local currentcam = game.Workspace.CurrentCamera

replicatedst.Camera.OnClientEvent:Connect(function()
	CameraInst.CameraType = Enum.CameraType.Scriptable
	CameraInst.CFrame = CameraPart
	--// Move cam
	local maxTilt = 10
	game:GetService("RunService").RenderStepped:Connect(function()
		CameraInst.CFrame = CameraPart * 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)


local UIS = game:GetService("UserInputService")
local KeyBind = "R"
UIS.InputBegan:Connect(function(input,Chatting)
	local camera = workspace.CurrentCamera
	if input.KeyCode == Enum.KeyCode.R then
		game.Workspace.CurrentCamera.CameraType = Enum.CameraType.Track -- Instead of 'Custom'
	end
end)

if it helps, i found the problem, but i don’t know how to fix.

It’s this part that is making the R not work

game:GetService("RunService").RenderStepped:Connect(function()
		CameraInst.CFrame = CameraPart * CFrame.Angles(
			math.rad((((mouse.Y - mouse.ViewSizeY / 2) / mouse.ViewSizeY)) * -maxTilt),
			math.rad((((mouse.X - mouse.ViewSizeX / 2) / mouse.ViewSizeX)) * -maxTilt),
			0
		)
	end)

Like i said, i don’t know how to fix… so good luck, hope it helped.

You need to disconnect RenderStepped so that the camera’s CFrame stops being modified.

local replicatedst = game:GetService("ReplicatedStorage")

local plr = game:GetService("Players").LocalPlayer
local mouse = plr:GetMouse()

local cameraPart = workspace.CameraPart.CFrame
local currentCamera = game.Workspace.CurrentCamera

local RenderSteppedConnection = nil

replicatedst.Camera.OnClientEvent:Connect(function()
	if (not RenderSteppedConnection) then
		--//
		currentCamera.CameraType = Enum.CameraType.Scriptable
		currentCamera.CameraSubject = cameraPart

		--// Move cam
		local maxTilt = 10
		RenderSteppedConnection = game:GetService("RunService").RenderStepped:Connect(function()
			currentCamera.CFrame = cameraPart * 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
end)


local UIS = game:GetService("UserInputService")
local KeyBind = Enum.KeyCode.R

UIS.InputBegan:Connect(function(input, Chatting)
	if (input.KeyCode == KeyBind) then
		if (typeof(RenderSteppedConnection) == "RBXScriptConnection") then
			RenderSteppedConnection:Disconnect() -- Disconnect RenderStepped
			RenderSteppedConnection = nil
		end
		
		local character = plr.Character
		if character and character:FindFirstChild("Humanoid") then
			currentCamera.CameraType = Enum.CameraType.Custom -- Set Custom
			currentCamera.CameraSubject = character.Humanoid
		end
	end
end)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.