Camera not continuously moving when holding key

Ok, So I already know the answer is probably right under my noes but heres the code

local mouse = game.Players.LocalPlayer:GetMouse()

while wait() do
	UserInputService.InputBegan:Connect(function(input, gameProcessed)
		
			if UserInputService:IsKeyDown(Enum.KeyCode.W) == true then
				game.Workspace.CameraPos.Position = game.Workspace.CameraPos.Position - Vector3.new(0,0,0.5)
			end
			if UserInputService:IsKeyDown(Enum.KeyCode.S) == true then
				game.Workspace.CameraPos.Position = game.Workspace.CameraPos.Position + Vector3.new(0,0,0.5)
			end
			if UserInputService:IsKeyDown(Enum.KeyCode.A) == true then
				game.Workspace.CameraPos.Position = game.Workspace.CameraPos.Position - Vector3.new(0.5,0,0)
			end
			if UserInputService:IsKeyDown(Enum.KeyCode.D) == true then
				game.Workspace.CameraPos.Position = game.Workspace.CameraPos.Position + Vector3.new(0.5,0,0)
			end
			mouse.WheelForward:Connect(function()
	   	 		game.Workspace.CameraPos.Position = game.Workspace.CameraPos.Position - Vector3.new(0,0.08,0)
			end)
			mouse.WheelBackward:Connect(function()
	   	 		game.Workspace.CameraPos.Position = game.Workspace.CameraPos.Position + Vector3.new(0,0.08,0)
			end)
		
	end)
end

The camera only moves once when a key is pressed and you have to keep pressing the key to move the camera. Help?

I get this error.

I don’t know if I’m doing something wrong, but I can’t see what your issue is because I am getting this error. And did you mean: game.Workspace.Camera.CFrame?

Few things you have wrong, CameraPos is a part that I am moving
And my control script is located here
image

1 Like

There seems to be a bit wrong here. Firstly, UserInputService.InputBegan only fires once, when a key is pressed and secondly you are using a key down in a event which isn’t really the proper use of it . Secondly i suggest not connecting InputBegan every time the while wait() do loops, as you are just connecting an unnecessary amount of functions. On that topic i would also not suggest connecting WheelForward/Backward every time a key is pressed either. it seems like you are creating some type of camera system ,in that case, i would suggest wrapping your code in a RenderStepped or HeartBeat instead and using some boolen logic.

Example:

local ADown = false
local RunService = game:GetService("RunService")


UserInputService.InputBegan:Connect(function(input, gameProcessed)
  if input.KeyCode==  Enum.KeyCode.A then
        ADown = true
   end
end)

UserInputService.InputEnded:Connect(function(input, gameProcessed)
  if input.KeyCode==  Enum.KeyCode.A then
        ADown = false
   end
end)


RunService.RenderStepped:Connect(function()
	if ADown then
       game.Workspace.CameraPos.Position = game.Workspace.CameraPos.Position - Vector3.new(0.5,0,0)
    end
end)
2 Likes