Help + issues with using HapticService?

Hello, I’m currently working on a line-runner style game and have tried adding haptic feedback; however, when trying to do so, I have been unsuccessful and haven’t found many posts about it on the forums and the documentation didn’t help a lot.

My game uses a custom camera system so I decided to try and link the vibrations to the magnitude of any camera shake effects. This is a sample of the code for mobile and controllers:

local Offset = Humanoid.CameraOffset
local Magnitude = Offset.Magnitude
if UserInputService.TouchEnabled == true then
	HapticService:SetMotor(Enum.UserInputType.Touch, Enum.VibrationMotor.Small, Magnitude)
elseif UserInputService.GamepadEnabled == true then
	HapticService:SetMotor(Enum.UserInputType.Gamepad1, Enum.VibrationMotor.RightTrigger, Magnitude)	
end

I was able to test this code on a PS5 controller and an iphone 11; however, I was unable to experience any haptic vibrations even after trying alternative values.

The only posts I was able to find on the matter was a feature request for haptic vibrations and news that the API was being reworked a few years ago.

I tried copying the code from this un-copylocked place; however, for some reason, it didn’t work in my game when I copied it over despite it working on the same iPhone 11 in the original game and me not changing any of the code.

This was the code that I copied:

local hapticService = game:GetService('HapticService')

local CAN_VIBRATE = hapticService:IsMotorSupported(Enum.UserInputType.Gamepad1, Enum.VibrationMotor.Small) -- phones use Gamepad1, probably as a placeholder but not sure if they're actually adding a mobile enum

if CAN_VIBRATE then -- if the device can vibrate
	local lastTap
	local VIBRATION_LENGTH = 1 / 60
	
	script.Parent.MouseButton1Click:Connect(function()
		local thisTick = tick() -- we need to save the time the user clicked in order to not have weird behaviour with vibration length, this is especially apparent with long vibration lengths
		lastTap = thisTick
		hapticService:SetMotor(Enum.UserInputType.Gamepad1, Enum.VibrationMotor.Small, 1)
		task.wait(VIBRATION_LENGTH)
		if lastTap == thisTick then -- now we check that the last input that was made was this event, if it was, it's safe to stop vibrating
			hapticService:SetMotor(Enum.UserInputType.Gamepad1, Enum.VibrationMotor.Small, 0)
		end
	end)
end

(The code was located inside a text button so I also placed it inside one)

While testing, I have found that the IsMotorSupported function returns true when tested on the iPhone; however, the SetMotor function only works in the older experience. Furthermore, I have also been unable to get it to work on the PS5 controller I have used test the feature with for consoles.

(I am using the controller with a PC and not a PS5 if that is relevant)

I am hesitant to post a bug report as I am unsure if it is a mistake on my behalf.

If anyone knows how I can use this feature, please let me know.
Thanks for reading!
Have a good day :slight_smile:

1 Like
local hapticService = game:GetService('HapticService')
local userInputService = game:GetService('UserInputService')
local Offset = Humanoid.CameraOffset
local Magnitude = Offset.Magnitude

local function applyHapticFeedback(motorType, intensity)
    hapticService:SetMotor(motorType, Enum.VibrationMotor.Small, intensity)
    task.wait(0.05) -- slight delay for noticeable feedback
    hapticService:SetMotor(motorType, Enum.VibrationMotor.Small, 0)
end

if userInputService.TouchEnabled and hapticService:IsMotorSupported(Enum.UserInputType.Touch, Enum.VibrationMotor.Small) then
    applyHapticFeedback(Enum.UserInputType.Touch, math.min(Magnitude / 5, 1))
elseif userInputService.GamepadEnabled and hapticService:IsMotorSupported(Enum.UserInputType.Gamepad1, Enum.VibrationMotor.RightTrigger) then
    applyHapticFeedback(Enum.UserInputType.Gamepad1, math.min(Magnitude / 5, 1))
end

Try running this code with fixed, high intensity values (like 0.5 to 1) to check if you feel anything on the devices. If so gradually adjust until you find a responsive range for your game.

If it does not work its prob cuz i incorrectly scripted this since i on mobile so yeah or its probably Hardware Limitations

I am unsure if this has already been fixed but try disabling voice chat if you have it enabled. This might fix the issue on mobile.

The rumble motors on playstation controllers specifically are known to not work on PC (mainly windows as far as I know). I usually use DS4 Windows when playing/testing with a controller if you would like to go the emulation route, or if you already have an xbox controller you can use that to.

Hope this helped :slight_smile:

Thanks! I turned off voicechat and that fixed the issue on mobile. Sadly it looks like roblox hasn’t fixed the bug so I might have to abandon one of the features.

Thanks for the info on playstation controllers as well. I’ll try to find an xbox controller to use in testing.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.