Yeah, I completely understand. There are 2 main reasons why I use it:
Consistency/Efficiency and Input Registering.
UserInputService/ContextActionService
can handle pretty much any input you throw at it. For consistency sakes, all you need is one service to achieve inputs such as the Mouse, Keyboard, Touch, Gamepad, etc.
Separating keyboard/mouse inputs into 2 different APIs like so;
local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")
local Player = Players.LocalPlayer
local Mouse = Player:GetMouse()
UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
if gameProcessedEvent then
return
end
if input.KeyCode == Enum.KeyCode.E then
print("Use E skill")
end
end)
Mouse.Button1Down:Connect(function()
print("Basic attack")
end)
Mouse.WheelForward:Connect(function()
print("Scroll up in inventory")
end)
Mouse.WheelBackward:Connect(function()
print("Scroll down in inventory")
end)
There is nothing wrong with using this method but the occasional might mouse events activating while you’re focused inside a TextBox or have the escape menu opened (need citation).
Instead, however, it is recommended (as even the roblox wiki recommends it), to use UserInputService/ContexActionService
for all types of inputs.
local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")
UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
if gameProcessedEvent then
return
end
if input.UserInputType == Enum.UserInputType.Keyboard then
if input.KeyCode == Enum.KeyCode.E then
print("Use E skill")
end
elseif input.UserInputType == Enum.UserInputType.MouseButton1 then
print("Basic attack")
end
end)
UserInputService.InputChanged:Connect(function(input, gameProcessedEvent)
if gameProcessedEvent then
return
end
if input.UserInputType == Enum.UserInputType.MouseWheel then
local direction = input.Position.Z > 0 and "up" or "down"
if direction == "up" then
print("Scroll up inventory")
elseif direction == "down" then
print("Scroll down inventory")
end
end
end)
It is much cleaner and easier to work with instead of having more multiple events with 2 different APIs. Say you wanted to have an event for Mouse Movement and right click? Or have touch/controller support? Instead of creating new events for those using the Mouse
API along with having to use UserInputService
anyways for touch/controller, you can simply add onto the already existing UserInputService
events.
Another example: say you have a debounce
to your inputs, such as you’re unable to attack while casting a spell, instead of copy pasting your if
checking, you only need to check it for your InputBegan
and InputChanged
.
local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")
local BusyState = false
UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
if gameProcessedEvent or BusyState then
return
end
BusyState = true
if input.UserInputType == Enum.UserInputType.Keyboard then
if input.KeyCode == Enum.KeyCode.E then
print("Use E skill")
wait(1)
end
elseif input.UserInputType == Enum.UserInputType.MouseButton1 then
print("Basic attack")
wait(1)
end
BusyState = false
end)
UserInputService.InputChanged:Connect(function(input, gameProcessedEvent)
if gameProcessedEvent then
return
end
if input.UserInputType == Enum.UserInputType.MouseWheel then
local direction = input.Position.Z > 0 and "up" or "down"
if direction == "up" then
print("Scroll up inventory")
elseif direction == "down" then
print("Scroll down inventory")
end
end
end)
The Mouse
API is superseded by UserInputService/ContextActionService
that has a more widespread use, so it’s good practice to use these 2 services. Source: