Moving a camera part with UIS:IsKeyDown()

I’m trying to make a part that moves the camera left and right when you press a key (A or D), like in Five Nights at Freddy’s or Not For Broadcast, where the operator’s “head” moves when you press A or D.

I’m using tweens to try and move the camera part to one of three positions: left, right and centre.

while userinputservice:IsKeyDown(Enum.KeyCode.A) do
	if camStatus == "Center" and Debounce == false then
		moveLeft()
		camStatus = "Left"
		Debounce = false
	elseif camStatus == "Left" and Debounce == false then
		Debounce = true
		resetPosition()
		camStatus = "Center"
		Debounce = false
	end
end

while userinputservice:IsKeyDown(Enum.KeyCode.D) do
	if camStatus == "Center" and Debounce == false then
		Debounce = true
		moveRight()
		camStatus = "Right"
		Debounce = false
	elseif camStatus == "Right" and Debounce == false then
		Debounce = true
		resetPosition()
		camStatus = "Center"
		Debounce = false
	end
end

This is the part that actually checks it the player is holding down the keys, and if so, tween to the right position.

The problem is, the part doesn’t move and no errors are shown.
If anyone has any solutions, I’d be grateful for the help.

local uis=game:GetService("UserInputService");
local Debounce = true
local camStatus = "Center"

uis.InputBegan:connect(function(input)
	if Debounce == true then
		if input.KeyCode==Enum.KeyCode.A and camStatus == "Center" then
			moveLeft()
			camStatus = "Left"
			Debounce = false
		end

		if input.KeyCode==Enum.KeyCode.D and camStatus == "Center" then
			moveRight()
			camStatus = "Right"
			Debounce = false
		end	
	end
end)


uis.InputEnded:connect(function(input)
	if Debounce == false then
		if input.KeyCode==Enum.KeyCode.A and camStatus == "Left" then
			resetPosition()
			camStatus = "Center"
			Debounce = true
		end

		if input.KeyCode==Enum.KeyCode.D and camStatus == "Right" then
			resetPosition()
			camStatus = "Center"
			Debounce = true
		end
	end
end)

Nothing happened with that code. Just to clarify this is in a LocalScript inside StarterPlayerScripts.

And how looks your functions: moveLeft,moveRight and resetPosition?

function moveLeft()
	print("beginning left")
	tweenModule.tweenModel(MovePart, cframes.left_tgp, .7, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut)
end

function moveRight()
	print("beginning right")
	tweenModule.tweenModel(MovePart, cframes.right_tgp, .7, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut)
end

function resetPosition()
	print("beginning reset")
	tweenModule.tweenModel(MovePart, cframes.origin, .7, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut)
end

Show me please full script with library

There’s nothing wrong with any other part of the script, just with the part where I check if the player is holding down the kay.

And isn’t there a function with while true in the script?

I’m not sure what you mean, or what that has to do with the issue.

Script what i gave you works, and you said the other parts of the your script work.
So the only problem there might be is a function with “while true” that would prevent button registration.

When you’re playing those tweens remember to add “Tween.Completed:Wait()” so that the thread yields until those tweens have completed.

This should help you. There’s no need to have separate functions for moving left, right, and center. Additionally, checking camStatus for a specific value is bad practice, because what if you want to add a fourth camera option? Now you need to add an entirely new check.

Just replace the elements of CAMERA_CFRAMES with whatever cframes you want:

local ContextActionService = game:GetService("ContextActionService")
local TweenService = game:GetService("TweenService")

local TWEEN_INFO: TweenInfo = TweenInfo.new(0.3, Enum.EasingStyle.Quad, Enum.EasingDirection.Out)
local CAMERA_CFRAMES: {CFrame} = {
	[1] = CFrame.new(0, 8, 0) * CFrame.Angles(0, math.rad(90), 0);
	[2] = CFrame.new(0, 8, 0);
	[3] = CFrame.new(0, 8, 0) * CFrame.Angles(0, math.rad(-90), 0);
}

local currentPosition: number = 1
local currentTween: Tween


local function actionHandler(actionName: string, inputState: Enum.UserInputState, inputObject: InputObject): Enum.ContextActionResult	
	local newPosition: number = currentPosition
	
	if inputState == Enum.UserInputState.Begin and currentTween == nil then
		if actionName == "MoveLeft" then
			newPosition = math.max(newPosition - 1, 1)
		elseif actionName == "MoveRight" then
			newPosition = math.min(newPosition + 1, #CAMERA_CFRAMES)
		end
	end
	
	if newPosition ~= currentPosition then
		currentTween = TweenService:Create(workspace.CurrentCamera, TWEEN_INFO, {CFrame = CAMERA_CFRAMES[newPosition]})
		currentTween.Completed:Connect(function()
			currentTween = nil
			currentTween:Destroy()
		end)
		
		currentPosition = newPosition
		currentTween:Play()
	end
	
	return Enum.ContextActionResult.Pass
end

ContextActionService:BindAction("MoveLeft", actionHandler, false, Enum.KeyCode.A)
ContextActionService:BindAction("MoveRight", actionHandler, false, Enum.KeyCode.D)
workspace.CurrentCamera:GetPropertyChangedSignal("CameraType"):Connect(function()
	workspace.CurrentCamera.CameraType = Enum.CameraType.Scriptable
	workspace.CurrentCamera.CFrame = CAMERA_CFRAMES[currentPosition]
end)