So, I am making a door that requires a lockpick to go through, in order to lockpick the door you need to hold in a ProximityPropmt for 5 seconds. I want the audio to begin as soon as the player interacts with the ProximityPrompt, not after 5 seconds. Does anybody know how to make this work?
Well, since ProximityPrompts don’t have any built-in events that fire as soon as you interact with it, you could just use UserInputService.
Now, I know what you may be thinking. “But if I press E any time, it’ll make the sound, right?” Indeed, so there are 2 ways to prevent this; ContextActionService and good ol’ attributes.
Since I don’t really use ContextActionService, I’ll just provide an example of using attributes.
Of course, attributes are basically values. If you like values better, just use those.
As said before, ProximityPrompts don’t have any built-in event handling the start of an interaction. However, it does fire a built-in event for when the prompt is first show, and when it is hidden. This is where the attribute comes in.
I don’t know what your game requires for code, so I won’t be able to provide any code without it being completely useless.
Anyways, set an attribute to true when the promptShown event is fired for the player, and vice versa. (The other event is promptHidden)
Now, onto UserInputService. Whenever you press “E” for the trigger, run a function that checks the attribute on the player wherever you put it, and if it is true, play the audio.
However, it’s not that simple. If the player stops interacting or walks away, the audio will still awkwardly play.
So, run another function for when input ends, check if it’s E and such, and just stop the audio.
For the player walking away, presuming the script handling the promptHidden event is the server, you can use a remoteEvent that fires the client, whenever the prompt is hidden. The client-end of the remoteEvent is intended to stop the audio, just like the player was ending the interaction early.
Note that this is all for a computer player. For mobile and xbox, you may need to include the equivalent buttons for E on those devices, such as taps and the X button.
I know this is sort of long, but I hope this helps!
try using :Triggered
instead of using :TriggerEnded
for the audio and use :TriggerEnded
for everything else separately.
prompt:Triggered:Connect(function()
-- sound here
end)
prompt:TriggerEnded:Connect(function()
-- your actual code here
end)
:Triggered only runs after the proximityprompt is held for 5 sec (the HoldDuration) though.
Triggered is fired after the interaction (after it completes), while @VEALYZ needs sound to play before the interaction.
Additonally, TriggerEnded is an event that fires when the interact doesn’t complete, or otherwise cancelled.
You can check at the documentation:
Did you not happen to see PromptButtonHoldBegan when you looked for and linked the documentation? In fact, Roblox’s example place about creating and using proximity prompts has the exact code in for what OP wants which is playing a sound while holding a prompt: Dungeon Delve.
Prompt UIs are built in Luau. They have events for detecting the moment a non-zero HoldDuration prompt is triggered. This is also how developers are able to build custom prompt UIs to begin with.
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.