HapticService

Show Deprecated
Not Creatable
Service
Not Replicated

The Xbox One controller and some other USB gamepad controllers have motors built in to provide haptic feedback. Adding rumbles and vibrations can greatly enhance a game's experience and provide subtle feedback that is hard to convey through visuals or audio.

Summary

Properties

Events

Methods

GetMotor(inputType: UserInputType, vibrationMotor: VibrationMotor): Tuple  

Returns the current vibration value set to the specified UserInputType and VibrationMotor. This will not return anything if SetMotor has not been called prior.

IsMotorSupported(inputType: UserInputType, vibrationMotor: VibrationMotor): boolean  

Returns true if the specified motor is available to be used with the specified UserInputType.


Returns true if the specified UserInputType supports haptic feedback.

SetMotor(inputType: UserInputType, vibrationMotor: VibrationMotor, vibrationValues: Tuple): void  

Sets the vibration intensity of the specified UserInputType and VibrationMotor.

Properties

Events

Methods

GetMotor

Returns the current vibration value set to the specified UserInputType and VibrationMotor. This will not return anything if SetMotor has not been called prior.

Parameters

inputType: UserInputType

The specified UserInputType.

vibrationMotor: VibrationMotor

The specified VibrationMotor.


Returns

The current vibration value set to the specified UserInputType and VibrationMotor or nil if HapticService#SetMotor has not been called prior.

IsMotorSupported

Returns true if the specified motor is available to be used with the specified UserInputType.

Parameters

inputType: UserInputType

The specific UserInputType being checked for VibrationMotor support.

vibrationMotor: VibrationMotor

The specified VibrationMotor checked to see if it supports the specified UserInputType.


Returns

True if the specified motor is available to be used with the specified UserInputType, false if not.

IsVibrationSupported

Returns true if the specified UserInputType supports haptic feedback.

Parameters

inputType: UserInputType

The specified UserInputType checked to see if it supports haptic feedback.


Returns

True if the specified UserInputType supports haptic feedback.

SetMotor

void

Sets the vibration intensity of the specified UserInputType and VibrationMotor.

Parameters

inputType: UserInputType

The specified UserInputType.

vibrationMotor: VibrationMotor

The specified VibrationMotor.

vibrationValues: Tuple

How intensely the motor should vibrate.


Returns

void

Code Samples

HapticService:SetMotor

1local UserInputService = game:GetService("UserInputService")
2local HapticService = game:GetService("HapticService")
3
4local cachedInputs = {} -- Note that we use a cache so we don't attach a Changed event more than once.
5
6local keyToVibration = {
7 [Enum.KeyCode.ButtonL2] = Enum.VibrationMotor.Small,
8 [Enum.KeyCode.ButtonR2] = Enum.VibrationMotor.Large,
9}
10
11local function onInputBegan(input)
12 if not cachedInputs[input] then
13 local inputType = input.UserInputType
14 if inputType.Name:find("Gamepad") then
15 local vibrationMotor = keyToVibration[input.KeyCode]
16 if vibrationMotor then
17 -- Watch this InputObject manually so we can accurately update the vibrationMotor.
18 local function onChanged(property)
19 if property == "Position" then
20 HapticService:SetMotor(inputType, vibrationMotor, input.Position.Z)
21 end
22 end
23 cachedInputs[input] = input.Changed:Connect(onChanged)
24 end
25 end
26 end
27end
28
29UserInputService.InputBegan:Connect(onInputBegan)