With UserInputService we can do alot of things, but some people make multiple functions. Whilst we can just do it with one!
I’ve looked around alot to actually find it to be possible to detect all input’s from: (PC, XBOX) in one function and then resume normally.
I did that with this code:
local userInputService = game:GetService("UserInputService")
function input(input)
local inputDown = false
if input.UserInputType == Enum.UserInputType.MouseButton1 then
inputDown = userInputService:IsMouseButtonPressed(Enum.UserInputType.MouseButton1) -- Checks if our mousebutton is down, 'MouseButton1' is our LMB and 'MouseButton2' is RMB
elseif input.UserInputType == Enum.UserInputType.Keyboard then
inputDown = userInputService:IsKeyDown(input.KeyCode) -- Checks if a key is down on our keyboard.
else
local states = userInputService:GetGamepadState(Enum.UserInputType.Gamepad1) -- This function get's our gamepad state, from here we can go through this table and look at all the states
for index, inputState in pairs(states) do -- This loop's through our table
if inputState.KeyCode == input.KeyCode then -- And then this checks if the keycode is the same as the keycode we are checking,
if inputState.UserInputState == Enum.UserInputState.Begin then -- if the 'InputState' is: 'Begin' then the 'inputDown' bool should be true! otherwise it has to be false.
inputDown = true
else
inputDown = false
end
end
end
end
-- detect inputs normally.
if input.KeyCode == Enum.KeyCode.E then
if inputDown then
-- down
else
-- not down
end
end
end
userInputService.InputBegan:Connect(input)
userInputService.InputEnded:Connect(input)