Click Detector Alarm Script Gives No Output

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve?
    I am attempted to create a script where, if a player presses the union button once, it starts a looped alarm sound. When they press it again, it stops the alarm sound.

  2. What is the issue?
    When I attempt to click the button in-game, nothing happens. Nothing in the output log either.

  3. What solutions have you tried so far?
    I have fixed all the output errors, and now that there aren’t any; I tried changing the click detector from a function to an if statement.

local alarm = script.Parent.Alarm
local button = script.Parent.Button

if script.Parent.Button.ClickDetector.MouseClick then
	wait(0.2)
	if alarm.IsPlaying == false then
		alarm.Playing = true
	end
	if alarm.IsPlaying == true then
	alarm.Playing = false
	end
end

Since there is no output, could I have made a crucial typo or command mistake?

1 Like

Does the Cursor change when you hover over the button?
Is the button Transparent? If it is then your click won’t activate it.

1 Like

Hello,

You should do this instead of putting the ClickDetector event in an if statement:

local alarm = script.Parent.Alarm
local button = script.Parent.Button

script.Parent.Button.ClickDetector.MouseClick:Connect(function()
	wait(0.2)
	if alarm.IsPlaying == false then
		alarm.Playing = true
	end
	if alarm.IsPlaying == true then
	alarm.Playing = false
	end
end)

You can’t do that in an if statement try the script that I gave you.

1 Like

The cursor does change to the ClickDetector image.
The button is not transparent.

The code you have written only runs once. It goes through the sequence and then completes. Instead do what @Nerkonl has mentioned and tie the code to an event. Events run a provided function every time it is occured, all you have to do is “Connect” the function to the event.

1 Like

Insted of alarm.Playing use :Play() and :Stop() should fix

1 Like

Did not work.

I tried changing the click detector from a function to an if statement.

I replaced, it still does not seem to work. No output error.

Current script:

local alarm = script.Parent.Alarm
local button = script.Parent.Button

script.Parent.Button.ClickDetector.MouseClick:Connect(function()
	wait(0.2)
	if alarm.IsPlaying == false then
		alarm:Play()
	end
	if alarm.IsPlaying == true then
	alarm:Stop()
	end
end)```

You have a ClickDetector in the Button part, I assume. Have you checked the properties of that ClickDetector? It may be disabled or have a really really short Click Distance.

1 Like

It is active, and I have set the MaxActivationDistance to 100.
No avail.

Your issue is in the IF statement.
Look at what happens:

  1. It waits
  2. It sees it is not playing
  3. It starts the alarm
  4. But then it sees the alarm is playing
  5. So it stops the alarm

Use an “elseif” statement. It will fix it
EDIT:
Replace the code with:

if alarm.IsPlaying == false then
	alarm:Play()
elseif alarm.IsPlaying == true then
    alarm:Stop()
end
1 Like

Thank you for the very detailed list and improvement. You are all very dedicated to helping out new developers. :smile:

No problem, if you ever have anymore questions just come back to the devforum. The community here is very nice.

However, I have an improvement for you, have you tried using alarm.Looped?

You could simply just say alarm.Looped = true inside of @GreekForge’s Script:

if alarm.IsPlaying == false then
    alarm.Looped = true
	alarm:Play()
elseif alarm.IsPlaying == true then
    alarm:Stop()
end

That could help.

1 Like

I have already set the Sound object to have Looped true; if that is what you are referring to.