How to play animation when holding down a Proximity prompt and when the hold duration ends, the animation stops

What I tried but it didn’t work:

local ProximityPromptService = game:GetService("ProximityPromptService")
local Anim = script:WaitForChild("Animation")
-- Detect when prompt hold begins
local function onPromptHoldBegan(player)
	Anim:Play()
end

-- Detect when prompt hold ends
local function onPromptHoldEnded(player)
	Anim:Stop()
end

Location of script:
image_2023-12-16_021303564

4 Likes

You can use ProximityPrompt.Triggered and ProximityPrompt.TriggerEnded

also you need to load the animation into the animator of the player

(This is for a local script btw)

local ProxPrompt = script.Parent.ProximityPrompt -- Your proximity prompt

local plr = game:GetService("Players").LocalPlayer -- we get the local player

local anim = nil -- create an animation variable

ProxPrompt.Triggered:Connect(function()
	anim = plr.Character.Humanoid.Animator:LoadAnimation(script.Animation) -- Load the animation
	
	anim:Play() -- Play the animation
	
end)


ProxPrompt.TriggerEnded:Connect(function()
	anim:Stop() -- Stop the animation
end)

- Edit -

I just saw your script is a ServerScript, preferably you want to play animations through LocalScripts, and LocalScripts go inside the player or the player’s character, but don’t worry there’s an easy fix to this

You can change your script’s run context to make it run on the client, just click your script, go to properties and change the RunContext to Client

image

(Keep in mind animations are usually played on the client, but if your script has other stuff that should be run in the server, this will not work correctly)
1 Like

Your script will error in line 1 and 8 because the ProximityPrompt or Animator might not have yet been created when the script runs so to fix you need to do:

local ProxPrompt = script.Parent:WaitForChild"ProximityPrompt" -- Your proximity prompt

local plr = game:GetService("Players").LocalPlayer -- we get the local player

local anim = (plr.Character or plr.CharacterAdded:Wait()):WaitForChild"Humanoid":WaitForChild"Animator":LoadAnimation(script:WaitForChild"Animation")

ProxPrompt.Triggered:Connect(function()
	anim:Play() -- Play the animation
end)


ProxPrompt.TriggerEnded:Connect(function()
	anim:Stop() -- Stop the animation
end)
1 Like

Actually it didn’t error

But I get what you mean, It is good to use WaitForChild()

2 Likes

Will other players see the animations

2 Likes

Yes, Animations can be seen by everyone even if they’re played from the Client.

2 Likes

Also the animation plays after the hold duration, I’m trying to make the animation play during the hold duration.

What it looks like currently:

2 Likes

This is probably what you are looking for.
If you are not using a duration on the proximity prompt, use this instead
script.Parent.Triggered:Connect(function(Player)
script.Parent.TriggerEnded:Connect(function(Player)

local CurrentTrack = {}

script.Parent.PromptButtonHoldBegan:Connect(function(Player)
	local Character = Player.Character
	local Humanoid = Character:WaitForChild("Humanoid")
	local Animator = Humanoid and Humanoid:WaitForChild("Animator")
	local Animation = Instance.new("Animation")

	Animation.AnimationId = "" --Your animation id
	CurrentTrack[Player.Name] = Animator:LoadAnimation(Animation)
	CurrentTrack[Player.Name].Looped = true
	CurrentTrack[Player.Name]:Play()
	
end)

script.Parent.PromptButtonHoldEnded:Connect(function(Player)
	CurrentTrack[Player.Name]:Stop()
end)

6 Likes

You need to connect your functions to the ProximityPrompt events so that they get triggered when the hold begins and ends.

local ProximityPromptService = game:GetService("ProximityPromptService")
local Anim = script:WaitForChild("Animation")

local proximityPrompt = script.Parent

local function onPromptHoldBegan(player)
	Anim:Play()
end

local function onPromptHoldEnded(player)
	Anim:Stop()
end

proximityPrompt.Triggered:Connect(onPromptHoldBegan)
proximityPrompt.Ended:Connect(onPromptHoldEnded)

The Triggered event fires when the player starts holding down the ProximityPrompt, and the Ended event fires when the player releases the prompt.

1 Like

It works! Thank you for helping :pray:

2 Likes

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