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
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
(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)
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)
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)
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.