PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience
Collapse Sidebar

SetMotor

Sets the vibration intensity of the specified UserInputType and VibrationMotor.

Parameters

Name Type Default Description

Returns

Return Type Summary

Code Samples


HapticService:SetMotor

This example makes the small motor vibrate depending on how much pressure is applied to the left trigger, and the large motor vibrate depending on how much pressure is applied to the right trigger.

local userInput = game:GetService("UserInputService")
local haptics = game:GetService("HapticService")

local cachedInputs = {} -- Note that we use a cache so we don't attach a Changed event more than once.

local keyToVibration =
{
	[Enum.KeyCode.ButtonL2] = Enum.VibrationMotor.Small;
	[Enum.KeyCode.ButtonR2] = Enum.VibrationMotor.Large;
}

local function onInputBegan(input)
	if not cachedInputs[input] then
		local inputType = input.UserInputType
		if inputType.Name:find("Gamepad") then -- If this is a Gamepad input.
			local vibrationMotor = keyToVibration[input.KeyCode]
			if vibrationMotor then
				-- Watch this InputObject manually so we can accurately update the vibrationMotor.
				local function onChanged(property)
					if property == "Position" then
						haptics:SetMotor(inputType,vibrationMotor,input.Position.Z)
					end
				end
				cachedInputs[input] = input.Changed:Connect(onChanged)
			end
		end
	end
end

userInput.InputBegan:Connect(onInputBegan)