Camera Script 'Breaking' when two buttons are pressed

ISSUE: Camera Script Breaks when two buttons are pressed (runservice continues to go on).
GOAL: When ALL keys are released, have the runService discontinue.
ATTEMPTS:

local loop = false
local count = 0
local connection
local PanKeys = {Enum.KeyCode.W, Enum.KeyCode.A, Enum.KeyCode.S, Enum.KeyCode.D}

spawn(function()
	userInputService.InputBegan:Connect(function(input, GP)
		if table.find(PanKeys,input.KeyCode) then
			if not GP and freeCamera == true then
				connection = runService.RenderStepped:Connect(function()
					print("Running.")
					local keysDown = userInputService:GetKeysPressed()
					cameraModule.Move(keysDown)
				end)
			end
		end
	end)
end)

spawn(function()
	
end)
userInputService.InputEnded:Connect(function(input, GP)
	if table.find(PanKeys, input.KeyCode) then
		if not GP and freeCamera == true then
			connection:Disconnect()
		end
	end
end)```

*The function of the camera (aka moving diagonally) still works when the bug occurs.

What’s the error in console when 2 keys are pressed?

There is no error. The runservice is supposed to stop when the keys are released. It does this when one key is pressed and released. The runservice continues to run.

Use GetKeysPressed() to determine if multiple keys are being held at any given time and act accordingly.

I’m going to do what the PlayerScripts do which is give each of the valid keys a binary value

Handle it accordingly based on key changes

local Keys = {
  [Enum.KeyCode.A] = 0
  ...
}

UserInputService.InputBegan:Connect(function(k, gp)
  if not gp and Keys[k.KeyCode] then
    Keys[k.KeyCode] = 1
  end
end)

UserInputService.InputEnded:Connect(function(k)
  if Keys[k.KeyCode] then
    Keys[k.KeyCode] = 0
  end
end)

By the way, keep your event binded the whole time and just react to key states as posted above

RunService.RenderStepped:Connect(function(delta)
  --react to the state inside Keys
  local panLeftSpeed = if Keys[Enum.KeyCode.A] then 5 * delta else 0


end)

Sorry ‘your binary values’ don’t quite make sense to me. Everything is being executed. It’s just that I wanted to TURN OFF the runservice for optimization reasons (aka when they aren’t doing anything).

Use ContextActionService, u can bind multiple keys to one function and check if they r held.