Script not stopping siren

For some reason when I do Click = not Clicked it won’t make it back to false meaning My siren sound won’t stop. On the first click it will make it true and siren will play but on the second click it isn’t stopping my siren sound

local Button = script.Parent

local Clicked = false

Button.MouseButton1Down:Connect(function()

	Clicked = not Clicked

	if Clicked == true then

		game.ReplicatedStorage.Siren:FireServer()

		if Clicked == false then

			game.ReplicatedStorage.Sirenstop:FireServer()
			end
	end

	end)

Your if statement checking if Clicked is equal to false is nested inside your other if statement that checks if Clicked is true, meaning that in reality you are only checking if Clicked is equal to true. Move your other if statement outside and then try again.

What do you mean by outside?? Like put an end in there??

It should be like this:

if Clicked == true then

    — Code here
    
end

if Clicked == false then

    — Code here

end

Like this

Button.MouseButton1Down:Connect(function()

	Clicked = not Clicked

	if Clicked == true then

		game.ReplicatedStorage.Siren:FireServer()

		end

		if Clicked == false then

		game.ReplicatedStorage.Sirenstop:FireServer()

		end

	end)

It still doesn’t work though

I think your logic is broken. You have if Clicked == false inside of if Clicked == true. The nested one will never run.

Ok and I added an end in there. see the previous reply

maybe:

local Button = script.Parent
Clicked = false

Button.MouseButton1Down:Connect(function()
       if Clicked == false then
Clicked = true
else
Clicked = false
end
      
      if Clicked == true then
             game.ReplicatedStorage.Siren:FireServer()
     else
             game.ReplicatedStorage.Sirenstop:FireServer()
      end
end)

You don’t actually need two remote-events for pausing and unpausing sirens! Try using something like this:

-- Toggle
local Button = script.Parent
local Siren = game:GetService('ReplicatedStorage'):WaitForChild('Siren');

Button.MouseButton1Down:Connect(function()
    Siren:FireServer()
end);
-- RemoteEvent
local Siren = game:GetService('ReplicatedStorage'):WaitForChild('Siren');
local SirenAudio -- put directory here

Siren.OnServerEvent:Connect(function()
    SirenAudio.Playing = not SirenAudio.Playing
end)
2 Likes

Thank you so much for your help!! It woks!!

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