How to make animation play on proximity prompt?

Hey robloxians!

This morning, I decided to try and create a animation that played when you activated a proximity prompt.

Been struggling for a few hours now and I can’t find any help on the web.
If anyone could help me with this then that’d be amazing!
For example, every time someone used a light switch, a animation would play on their character.

Thanks,
Cheers!

3 Likes

Using proximitypromptservice, you can create a serverscript that detects if a prompt has been triggered. The parameters are the obj and the player so your code would look something like this:

local proxService = game:GetService("ProximityPromptService")

proxService.PromptTriggered:Connect(function(obj, player)
	
	if obj.Parent.Name == "ProximityPart" then
[...]

From there you can get the character and load and play the animation.

Note that your obj.Parent is the lightswitch or an attachment. I personally rename this as my game has multiple uses for proximityprompts and it’s a way of seperating them from each other.

4 Likes

Have you done anything yet? If you have, could you please show it so we can know where you are currently at?

There is an event called ProximityPrompt.Triggered. This event fires when the prompt has been activated when you tap or press the specific key that triggers the prompt. So to play an animation, you can just call the .Triggered event and play the animation.

Here is a code example (make sure your script is a server script, and is under the proximity prompt that you want players to activate to play an animation):

local ProximityPrompt = script.Parent
local Anim = script:WaitForChild("Animation") -- assuming that your animation is a child of the script

ProximityPrompt.Triggered:Connect(function(plr) -- gets the player
	local hum = plr.Character:WaitForChild("Humanoid") -- gets the player's humanoid
	local loadAnim = hum.Animator:LoadAnimation(Anim) -- loads animation
	
	loadAnim:Play() -- plays the animation
end)

Although there are other ways to detect players activating proximity prompts, I find this as a more simpler way of doing this to begin with.

15 Likes