Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do you use HapticService to make the player's controller vibrate when dead?

I was working on a game where it is mainly centered around the xbox controller. I thought it would be cool for the gamepad to rumble when you die, so I looked on the roblox wiki and made a localscript that looks like this:

local plr = game.Players.LocalPlayer
local haptics = game:GetService("HapticService")
plr.Character.Humanoid.Died:connect(function()
haptics:SetMotor(
 Enum.UserInputType.Gamepad1,
 Enum.VibrationMotor.Large,
 0.8)
wait(0.4)
haptics:SetMotor(
 Enum.UserInputType.Gamepad1,
 Enum.VibrationMotor.Large,
 0)		
end)

It worked fine in the testing mode for studio, but when I tried it in my actual server, it did not work and it showed this error message:

Players.TroytheDestroyer.Backpack.Vibration:3: attempt to index field "Character" (a nil value)

I have worked with the property "Character" in a localscript before, but now all of a sudden it doesn't work.

Thank you!

2 answers

Sometimes localscripts load before the game's models load. As such, you should use WaitForChild frequently when looking for children of the game. Here is the end script:

local plr = game.Players.LocalPlayer
local haptics = game:GetService("HapticService")

plr:WaitForChild("Character").Humanoid.Died:connect(function()
	haptics:SetMotor(Enum.UserInputType.Gamepad1, Enum.VibrationMotor.Large, 0.8)
	wait(0.4)
	haptics:SetMotor(Enum.UserInputType.Gamepad1, Enum.VibrationMotor.Large, 0)     
end)

have you committed your scripts when you published the game?

This discussion is preserved as it was. New answers, comments, and votes are closed.