Z Keybind Not Working

What I’m trying to do Is make It so well the user holds down the Z key It will zoom In, similar to the Minecraft mod Optifine.

At first It was not working with no errors printed, so I added some print statements In the functions of which nothing was triggered upon testing. This also rules out the possibility of It just being something like the tween not working.

local uis = game:GetService("UserInputService")
local ts = game:GetService("TweenService")

uis.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.KeyCode.Z then
		print("before tween")
		local ti = TweenInfo.new(0.5, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, 0, false)
		local pt = {
			FieldOfView = 30
		}
		local tween = ts:Create(game.Workspace.Camera, ti, pt)
		tween:Play()
		print("after tween")
	end
end)

uis.InputEnded:Connect(function(input)
	if input.UserInputType == Enum.KeyCode.Z then
		print("end begin")
		local ti = TweenInfo.new(0.5, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, 0, false)
		local pt = {
			FieldOfView = 70
		}
		local tween = ts:Create(game.Workspace.Camera, ti, pt)
		tween:Play()
		print("end end")
	end
end)

use input.KeyCode not input.UserInputType

if input.KeyCode == Enum.KeyCode.Z then
 -- user pressed z
end

also check to make sure the user is not typing or something so there are no accidental key presses
the second argument, processed, is true when the user is doing something where they arent playing the game like chatting or in the report menu

uis.InputBegan:Connect(function(input,processed)
  if processed then return end -- do nothing if processed 
  if input.KeyCode == Enum.KeyCode.Z then
   -- user pressed z
  end
end)
1 Like

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