Using Keyboard instead of mouse

Hi everyone.

I am trying to create a gun which shoots when “Z” is pressed, as the mouse-button will be used for other functions. The mouse part is working, but nothing happens when I press Z, because:
UserInputType is not a valid member of Mouse “Instance”

The bullet will be flying in the direction of the cursor, which is why I still need the mouse in my function. Is there a better (functional) way to do this?

This is my script which fires the bullet:

Blockquote

gun.Equipped:Connect(function(input,gameProcessed,mouse)
if not gameProcessed then
if input.UserInputType == Enum.UserInputType.Keyboard then
local keycode = input.KeyCode
if keycode == Enum.KeyCode.z then
if gun.Ammo.Value > 0 then
remoteEventMusket:FireServer(gun.Handle.Position,mouse.Hit.p)
print(“musket fired”)
gun_shot:Play()
gun.Ammo.Value = gun.Ammo.Value - 1
else
gun_empty:Play()
end
end
wait(4)
gun.Ammo.Value = 2
end
end
end)

Blockquote

The equipped event doesn’t return user input services parameters like input, gameProcessed and such.

You will need to method one keep the UIs connection and use a bool to detect if the tool is equipped or not.

The UIS.InputBegan event is what returns the keycode and input data like below:

And the bool is in the solution to detect if the tool is equipped or not.

Or use CAS which I prefer:

1 Like