How to rotate camera while holding a button

Basically, all I need is make the camera rotate while the player is pressing buttons. So when the player presses A, camera goes to the left for the time while the button is held.


I’m pretty sure its supposed to be done with CFrame.Angles, and I would do it myself, but I have no idea how to make it turn only while the button is held

local Game = game
local Workspace = workspace
local Camera = Workspace.CurrentCamera
local RunService = Game:GetService("RunService")
local Players = Game:GetService("Players")
local LocalPlayer = Players.LocalPlayer

local Connection, Direction

local function OnRenderStep()
	if not Direction then Connection:Disconnect() end
	Camera.CFrame *= CFrame.fromOrientation(0, if Direction == "Left" then math.rad(1) elseif Direction == "Right" then math.rad(-1) else 0, 0)
end

local function OnLeftMouseDown()
	Direction = "Left"
	Connection = RunService.RenderStepped:Connect(OnRenderStep)
end

local function OnLeftMouseUp()
	Direction = nil
end

local function OnRightMouseDown()
	Direction = "Right"
	Connection = RunService.RenderStepped:Connect(OnRenderStep)
end

local function OnRightMouseUp()
	Direction = nil
end

local ScreenGui = Instance.new("ScreenGui")

local LeftButton = Instance.new("TextButton")
LeftButton.Position = UDim2.new(0.425, 0, 0.45, 0)
LeftButton.Size = UDim2.new(0.05, 0, 0.1, 0)
LeftButton.Text = "Left"
LeftButton.MouseButton1Down:Connect(OnLeftMouseDown)
LeftButton.MouseButton1Up:Connect(OnLeftMouseUp)
LeftButton.Parent = ScreenGui

local RightButton = Instance.new("TextButton")
RightButton.Position = UDim2.new(0.525, 0, 0.45, 0)
RightButton.Size = UDim2.new(0.05, 0, 0.1, 0)
RightButton.Text = "Right"
RightButton.MouseButton1Down:Connect(OnRightMouseDown)
RightButton.MouseButton1Up:Connect(OnRightMouseUp)
RightButton.Parent = ScreenGui

local PlayerGui = LocalPlayer:WaitForChild("PlayerGui")
ScreenGui.Parent = PlayerGui

Here’s an example script for you to take inspiration from.
https://gyazo.com/c2080d2ed0ee46cdcb88afa18b140175

3 Likes