How can I make a GUI button play a different audio if pressed for longer?

I would like to state that I’m not well acquainted with Lua and that I’m working on this small project to try and improve.

What I’m currently working on is a game focused on morse code. At the moment, I have a GUI text button that when is clicked, it plays an audio (just a short beep/morse code dot).

Here is the script I’ve made for it to play the audio:

local MorseButton = script.Parent
local BeepSFX = script.Parent.Sound

MorseButton.MouseButton1Click:Connect(function()
	BeepSFX:Play()
end)

What I’m trying to achieve is to be able to play both dots and dashes (short beep and longer beep) on the same GUI text button which is dependent on how long the key is held down for.

If the key was simply clicked, I need it to play a dot/short beep.
If the key was held down for longer than just a click, I need it to play a dash/longer beep.

How would I go about doing that?
How can my text button be scripted so that it can detect whether or not the key was held down for longer?
Would I need two separate audios for the dot and dash or would one suffice?

Use UserInputService and the InputBegan and InputEnded events to check how long they’ve held on for, and when they let go.

One thing that you can do is to use Button1Down, and then have the sound on looped and then just play the sound from there. And then use the Button1Up event and then you can just stop the sound playing.

This would mean that it would play for how long u hold the button down, so holding it for a short time would play a short beep, holding it for a long time would make it play a long beep.

3 Likes

I didn’t think of that, thanks.

You could try a loop to set a time depending how long they hold
Building on to what @ethann_n said you could use MouseButton1Down and MouseButton1Up to determine this.

local Button = script.Parent
local BeepSFX = script.Parent.Sound

local Holding = false
local HoldTime = 0

Button.MouseButton1Down:Connect(function()

    Holding = true

end)

Button.MouseButton1Up:Connect(function()

    Holding = false

    if HoldTime <= 1 then

        BeepSFX:Play()

    elseif HoldTime > 1 then

        -- Play Other Sound

    end

end)

task.spawn(function()

    while Holding == true do

        HoldTime += 0.1
        task.wait(0.1) -- You can change the increments depending how you want how fast / slow for it to count!

    end

end)
3 Likes

Exactly what I needed, much appreciation.

No worries! Glad to help someone out! I hope you enjoy your day also if it works like you need make sure to mark it as solution! :smiley: