Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

My GUI Button doesn't work when I click on. The function given doesn't activate, any errors?

Heres the script:

local Button = script.Parent
lightbar1 = script
lightbar2 = script
redlight = lightbar1
bluelight = lightbar2

Button.MouseButton1Click:connect(function()
	while true do
	lightbar1.redlight.Enabled = true
	wait(0.01)
	lightbar2.bluelight.Enabled = true
	lightbar1.redlight.Enabled = false
	wait(0.01)
	lightbar1.redlight.Enabled = true
	lightbar2.bluelight.Enabled = false
	
if Button.MouseButton2Click:connect(function()
	lightbar1.redlight.Enabled = false
	lightbar2.bluelight.Enabled = false
end)

Basically this GUI Button, when clicked, should enable these two surface lights, and if its clicked again, then it stops. I haven't seen any errors in the script, but it still doesn't work when I click the button, any errors in this script?

1 answer

###The problem with your script is that you're trying to listen for an event within an if statement, which is bound to throw you an error. You're nesting your events inside other events, which can cause weird behaviour. Also, your while loop runs indefinitely, regardless if you disable the surface light. You'll need to break the loop.

local Button = script.Parent
local lightbar1 = script
local lightbar2 = script
local redlight = lightbar1
local bluelight = lightbar2
loca on = false

Button.MouseButton1Click:Connect(function()
    -- switch to :Connect, :connect is deprecated 
    on = not on

    while on do
        lightbar1.redlight.Enabled = true
        wait(0.01)
        lightbar2.bluelight.Enabled = true
        lightbar1.redlight.Enabled = false

        wait(0.01)

        lightbar1.redlight.Enabled = true
        lightbar2.bluelight.Enabled = false

    if not on then
        on = false 
        lightbar2.bluelight.Enabled = false
    end 
end)


###On a side note, switch to Connect, as connect is deprecated and should not be used in new work.

This discussion is preserved as it was. New answers, comments, and votes are closed.