Playing animation with "PromptButtonHoldBegan" and "PromptButtonHoldEnded"

I have a script that requires the player to play an animation while the proximity prompt has its PromptButtonHoldBegan event triggered and stop the said animation when PrompButtonHoldEnded event is triggered.

The issue is that it plays the animation when the PromptButtonHoldBegan event is triggered but it does not stop upon the PromptButtonHoldEnded event being triggered.

And yes, I have tried to use forum posts to solve my issue but it had no success.

Here’s the script:

local prox = script.Parent

prox.PromptButtonHoldBegan:Connect(function(plr)
	local hum = plr.Character.Humanoid
	local anim = hum:LoadAnimation(script.Animation)
	anim:Play()
	hum.Parent.HumanoidRootPart.Anchored = true
end)

prox.PromptButtonHoldEnded:Connect(function(plr)
	local hum = plr.Character.Humanoid
	local anim = hum:LoadAnimation(script.Animation)
	anim:Stop()
	hum.Parent.HumanoidRootPart.Anchored = false
end)

Any sort of help is greatly appreciated :smile:

1 Like

Try this :slight_smile:

local prox = script.Parent
local anims = {}
prox.PromptButtonHoldBegan:Connect(function(plr)
	local hum = plr.Character.Humanoid
	local anim = hum:LoadAnimation(script.Animation)
	anim:Play()
    table.insert(anims, {plr.UserId, anim})
	hum.Parent.HumanoidRootPart.Anchored = true
end)

prox.PromptButtonHoldEnded:Connect(function(plr)
	local hum = plr.Character.Humanoid
    for i,v in pairs(anims) do
        if v[1] == plr.UserId then
            v[2]:Stop()
            table.remove(anims, i)
            break
        end
    end
	hum.Parent.HumanoidRootPart.Anchored = false
end)
3 Likes

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