Camera movement not moving repeatedly when holding down a certain arrow key

Hello Developers!

I’ve created a Local-Script under StarterCharacter called “2DCameraMovment”. The script function is to replicate those games where rather than the camera following your character, it allows you to simply move your camera freely, such as Lobotomy Corporation. The issue? It has to do with the camera move functions, it does move the camera, although it doesn’t repeatedly move while it’s corresponding arrow key is pressed. (For example, if I press the Left Arrow Key, the camera moves left but after a brief moment, it won’t move despite me holding the Left Arrow Key.)

local Camera = workspace.CurrentCamera
Camera.FieldOfView = 80
--Char--
local char = script.Parent
local hrp = char:WaitForChild("HumanoidRootPart")
--Services--
local CAS = game:GetService("ContextActionService")
local UIS = game:GetService("UserInputService")
local TS = game:GetService("TweenService")
local RunS = game:GetService("RunService")
CAS:UnbindAction("moveForwardAction")
CAS:UnbindAction("moveBackwardAction")
CAS:UnbindAction("moveLeftAction")
CAS:UnbindAction("moveRightAction")
--Functions--
local function MoveCameraLeft()
	local Goal = {
		CFrame = Camera.CFrame * CFrame.new(-30, 0, 0)
	}
	local Tweeninfo = TweenInfo.new(
		1,
		Enum.EasingStyle.Sine,
		Enum.EasingDirection.Out,
		0,
		false,
		0
	)
	local Tween = TS:Create(Camera,Tweeninfo,Goal)
	Tween:Play()
	task.wait()
end
local function MoveCameraRight()
	local Goal = {
		CFrame = Camera.CFrame * CFrame.new(30, 0, 0)
	}
	local Tweeninfo = TweenInfo.new(
		1,
		Enum.EasingStyle.Sine,
		Enum.EasingDirection.Out,
		0,
		false,
		0
	)
	local Tween = TS:Create(Camera,Tweeninfo,Goal)
	Tween:Play()
	task.wait()
end
local function MoveCameraUp()
	local Goal = {
		CFrame = Camera.CFrame * CFrame.new(0, 30, 0)
	}
	local Tweeninfo = TweenInfo.new(
		1,
		Enum.EasingStyle.Sine,
		Enum.EasingDirection.Out,
		0,
		false,
		0
	)
	local Tween = TS:Create(Camera,Tweeninfo,Goal)
	Tween:Play()
	task.wait()
end
local function MoveCameraDown()
	local Goal = {
		CFrame = Camera.CFrame * CFrame.new(0, -30, 0)
	}
	local Tweeninfo = TweenInfo.new(
		1,
		Enum.EasingStyle.Sine,
		Enum.EasingDirection.Out,
		0,
		false,
		0
	)
	local Tween = TS:Create(Camera,Tweeninfo,Goal)
	Tween:Play()
	task.wait()
end
RunS.RenderStepped:Connect(function()
	Camera.CameraType = Enum.CameraType.Scriptable
	--Camera.CFrame = Camera.CFrame:Lerp(CFrame.new(hrp.Position + Vector3.new(0, 5, 10), hrp.Position), 0.1)
	UIS.InputBegan:Connect(function(Input, gameProcessedEvent)
		if Input.UserInputType == Enum.UserInputType.Keyboard then
			if Input.KeyCode == Enum.KeyCode.Left then
				if UIS:IsKeyDown(Enum.KeyCode.Left) then
					MoveCameraLeft()
					task.wait()
				end
			end
			if Input.KeyCode == Enum.KeyCode.Right then
				if UIS:IsKeyDown(Enum.KeyCode.Right) then
					MoveCameraRight()
					task.wait()
				end
			end
			if Input.KeyCode == Enum.KeyCode.Up then
				if UIS:IsKeyDown(Enum.KeyCode.Up) then
					MoveCameraUp()
					task.wait()
				end
			end
			if Input.KeyCode == Enum.KeyCode.Down then
				if UIS:IsKeyDown(Enum.KeyCode.Down) then
					MoveCameraDown()
					task.wait()
				end
			end
		end
	end)
end)

You need to create a loop that runs while the key is down, currently it moves the camera once and it doesn’t know it has to move it again while the key is down

This is pretty simple to fix, just use UserInputService:IsKeyDown in a repeat loop to check if the key is still being held (and use task.wait, we dont want infinite loops here)

repeat
  -- camera code
  task.wait()
until not UserInputService:IsKeyDown(Enum.KeyCode.A)
1 Like

In all honesty I would just be moving the camera to the parts position on render stepped. I’m working on a game with code like this and that’s my method.

Thanks! It actually works.

[Filler due to ROBLOX needing 30 letters.]

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