Input service 2
A Simple module for input
What is it?
Input Service 2 is a module that utilizes the InputBegan and InputEnded
features from UserInputService. Using the functions from this module allows you to easily
simulate clicks, change movement keybinds, map keys to other keys, and more.
Links:
How to use:
Input Service 2 comes with 6 functions:
Functions
BindMovement
.BindMovement(Keys) -- Optional Parameter
This sets up the module, this must be done before using. The Keys parameter is a table of keyCodes for character movement, the default is:
{ Enum.KeyCode.Space, Enum.KeyCode.W, Enum.KeyCode.A, Enum.KeyCode.S, Enum.KeyCode.D }
BindMovementKey
.BindMovementKey(bind : Enum.KeyCode, replace : Enum.KeyCode)
This functions ReBinds a movement key, bind being the new key and replace being the default key from the function above
MapKey
.MapKey(bind : Enum.KeyCode, replace : Enum.KeyCode)
Similar to the previous function, this one fires the input of bind
when ever replace
is clicked
Click
.Click(Name : string, holdDuration : number)
As the name suggests, this simulates a click of the key Enum.KeyCode[Name]
and the holdDuration
changes how long the click is held
InputBegan and InputEnded
.InputBegan:Connect(function(Input, GameProcessed) end)
.InputEnded:Connect(function(Input, GameProcessed) end)
These functions behave the same as the original UserInputService.InputBegan
but it also recieves simulated inputs. Useful for when using a keybind that will be simulated through other methods.
Example Usage:
In this example, we will simulate walking forward when a button is held, and switch to left hand movement keybinds (I,J,K,L) when the player clicks another button.
Process:
- First step is to grab the module script and parent it to
ReplicatedStorage
- Next we’ll create a
LocalScript
inStarterPlayerScripts
called “InputHandler” - Next we’ll add a
ScreenGui
toStarterGui
with 2 buttons called “LeftHand” and “MoveForward”:
- Now time for the coding. In “InputHandler” we will require the module, set variables and begin the set up for Input Service 2:
local InputService2 = require(game.ReplicatedStorage.InputService2)
local PlayerGui = game.Players.LocalPlayer.PlayerGui
local ScreenGui = PlayerGui.ScreenGui
local LeftHand = ScreenGui.LeftHand
local MoveForward = ScreenGui.MoveForward
local usingLeftHand = false
local leftHandKeys = {
Enum.KeyCode.Space,
Enum.KeyCode.I,
Enum.KeyCode.J,
Enum.KeyCode.K,
Enum.KeyCode.L
} -- Table of left hand controls
InputService2.BindMovement() -- begin setting up the module, Keys are WASD by default
- Now that the set up is done, We can finish by getting the input from the button clicks and simulating clicks from there.
local InputService2 = require(game.ReplicatedStorage.InputService2)
local PlayerGui = game.Players.LocalPlayer.PlayerGui
local ScreenGui = PlayerGui:WaitForChild("ScreenGui")
local LeftHand = ScreenGui.LeftHand
local MoveForward = ScreenGui.MoveForward
local usingLeftHand = false
local leftHandKeys = {
Enum.KeyCode.Space,
Enum.KeyCode.I,
Enum.KeyCode.J,
Enum.KeyCode.K,
Enum.KeyCode.L
} -- Table of left hand controls
InputService2.BindMovement() -- begin setting up the module, Keys are WASD by default
LeftHand.MouseButton1Click:Connect(function()
usingLeftHand = not usingLeftHand
InputService2.BindMovement(usingLeftHand and leftHandKeys or nil) -- Rebind the movement with the corresponding keys
end)
MoveForward.MouseButton1Down:Connect(function()
local key = usingLeftHand and "I" or "W"
InputService2.SimulateInputBegan(key) -- Simulate the input beggining for the key
end)
MoveForward.MouseButton1Up:Connect(function()
local key = usingLeftHand and "I" or "W"
InputService2.SimulateInputEnded(key) -- Simulate the input ending for the key
end)
And just like that, it’s working, simple and easy!
If you have any feedback or you have found an error please let me know, Thanks.