Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I make the button on and off when clicking it?

Asked by 6 years ago

I made script of when you press GUI button it starts police siren. It works fine and well but the only problem you can't stop it. How do I make so that when you click button again, it stops the sirens and the lights too.

Here's my script:

local light = game.Workspace.Lights

function onClicked()   
    light.Sound:Play()
    while true do
        light.Red.Material = "Neon"
        light.Blue.Material = "Plastic"
        light.Red.PointLight.Enabled = true
        light.Blue.PointLight.Enabled = false
        wait(0.8) 
        light.Blue.Material = "Neon"
        light.Red.Material = "Plastic"
        light.Red.PointLight.Enabled = false
        light.Blue.PointLight.Enabled = true
        wait(0.8)
    end
end

script.Parent.TextButton.MouseButton1Down:connect(onClicked)


1 answer

Log in to vote
0
Answered by 6 years ago

Firstly, if you have filtering enabled on reply to this saying you are as this will not work. In the model put a BoolValue. This will be your yes and no value. Once you have placed that in, use this script.

local light = game.Workspace.Lights

function onClicked()   -- Your on and off script.
    if script.Parent.BoolValue.Value == false then
        script.Parent.BoolValue.Value = true
    elseif script.Parent.BoolValue.Value == true then
        script.Parent.BoolValue.Value = false
    end
end

script.Parent.BoolValue.Changed:connect(function()
    if script.Parent.BoolValue.Value == true then
        light.Sound:Play()
        while script.Parent.BoolValue.Value == true do
            light.Red.Material = "Neon"
            light.Blue.Material = "Plastic"
            light.Red.PointLight.Enabled = true
            light.Blue.PointLight.Enabled = false
            wait(0.8) 
            light.Blue.Material = "Neon"
            light.Red.Material = "Plastic"
            light.Red.PointLight.Enabled = false
            light.Blue.PointLight.Enabled = true
            wait(0.8)
        until script.Parent.BoolValue.Value == false
        light.Sound:Stop()
        end
    end
end)

script.Parent.TextButton.MouseButton1Down:connect(onClicked)

That theoretically should work.

0
It doesn't work at all now. ShadowNinja1080 6 — 6y
0
Filtering Enabled on? Any errors? MachoPiggies 526 — 6y
Ad

Answer this question