How to replace Mousebutton, with Key userinput

so i have a function which will run, if pressed by mousebutton
but i wanted to make it use as Keyboard, and mine didnt work

and how do i make it work on both, so Mousebutton and UserinputKeys

rightArrow.MouseButton1Click:Connect(function() -- replcae this with inputkeys
		if number == 3 then -- Chage 3 depending on the amount of cameras you have.
			cam.CFrame = camFolder["1"].CFrame
			number = 1
		else
			cam.CFrame = camFolder[number +1].CFrame
			number = number +1
		end
		wait(.1)
	end)
end)
	
	leftArrow.MouseButton1Click:Connect(function() -- replcae this with inputkeys
		if number == 1 then
		cam.CFrame = camFolder["3"].CFrame -- Chage 3 depending on the amount of cameras you have.
			number = 3
		else
			cam.CFrame = camFolder[number -1].CFrame
			number = number -1
		end
		wait(.1)
	end)

You can make use of the UserInputService.

Example Usage:

local UserInputService = game:GetService("UserInputService")

local function onInputBegan(input, _gameProcessed)
	if input.KeyCode == Enum.KeyCode.G then
		-- do whatever
	end
end

UserInputService.InputBegan:Connect(onInputBegan)

You can also change it to InputEnded if you prefer it to be when the key is lifted up, rather than initially pushed down.

ive tried that and didnt work, could you show me how it work on this script

Try this, this uses InputEnded rather than InputBegan. Keep in mind, I’m utilizing the code you have provided, so you might need to alter this to your wants and needs. Also the keys for this to work are Q and E.

local UserInputService = game:GetService("UserInputService")

local function onInputEnded(input, _gameProcessed)
	if _gameProcessed then return end
	if input.KeyCode == Enum.KeyCode.Q then
		if number == 1 then
			cam.CFrame = camFolder["3"].CFrame -- Chage 3 depending on the amount of cameras you have.
			number = 3
		else
			cam.CFrame = camFolder[number -1].CFrame
			number = number -1
		end
		wait(.1)
	end
	if input.KeyCode == Enum.KeyCode.E then
		if number == 3 then -- Chage 3 depending on the amount of cameras you have.
			cam.CFrame = camFolder["1"].CFrame
			number = 1
		else
			cam.CFrame = camFolder[number +1].CFrame
			number = number +1
		end
		wait(.1)
	end
end

UserInputService.InputEnded:Connect(onInputEnded)

This would go inside the same LocalScript presumably.

1 Like