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

Where exactly did I go wrong with this on/off script?

Asked by 10 years ago
local on = false

script.Parent.MouseButton1Down:connect(onClicked)
    if not on then
        on = true
    elseif on then
        on = false
    end
end)

while on do 
    game.Workspace.Test4.Light1.BrickColor = BrickColor.new("Medium stone grey") 
    game.Workspace.Test4.Light2.BrickColor = BrickColor.new("Really red")  
    game.Workspace.Test4.Light3.BrickColor = BrickColor.new("Medium stone grey")
    game.Workspace.Test4.Light4.BrickColor = BrickColor.new("Really red")
    game.Workspace.Test4.Light5.BrickColor = BrickColor.new("Really red")
    game.Workspace.Test4.Light6.BrickColor = BrickColor.new("Really red")
    wait(1)
    game.Workspace.Test4.Light1.BrickColor = BrickColor.new("Really red") 
    game.Workspace.Test4.Light2.BrickColor = BrickColor.new("Medium stone grey")
    game.Workspace.Test4.Light3.BrickColor = BrickColor.new("Really red")
    game.Workspace.Test4.Light4.BrickColor = BrickColor.new("Medium stone grey")
    game.Workspace.Test4.Light5.BrickColor = BrickColor.new("Medium stone grey")
    game.Workspace.Test4.Light6.BrickColor = BrickColor.new("Medium stone grey")
    wait(1)
    game.Workspace.Test4.Light1.BrickColor = BrickColor.new("Medium stone grey") 
    game.Workspace.Test4.Light2.BrickColor = BrickColor.new("Medium stone grey") 
    game.Workspace.Test4.Light3.BrickColor = BrickColor.new("Medium stone grey")
    game.Workspace.Test4.Light4.BrickColor = BrickColor.new("Medium stone grey") 
    game.Workspace.Test4.Light5.BrickColor = BrickColor.new("Medium stone grey")
    game.Workspace.Test4.Light6.BrickColor = BrickColor.new("Medium stone grey")
    wait(1)
end

while not on do 
    game.Workspace.Test4.Light1.BrickColor = BrickColor.new("Medium stone grey") 
    game.Workspace.Test4.Light2.BrickColor = BrickColor.new("Medium stone grey")  
    game.Workspace.Test4.Light3.BrickColor = BrickColor.new("Medium stone grey")
    game.Workspace.Test4.Light4.BrickColor = BrickColor.new("Medium stone grey")
    game.Workspace.Test4.Light5.BrickColor = BrickColor.new("Medium stone grey")
    game.Workspace.Test4.Light6.BrickColor = BrickColor.new("Medium stone grey")
    wait()
end 
0
I do see some errors, but before anything else, what did the output say? IcyArticunoX 355 — 10y
0
17:30:36.161 - Auto-Saving... 17:35:36.230 - Auto-Saving... 17:40:36.298 - Auto-Saving... 17:45:33.116 - Auto-Saving... 17:45:36.365 - Auto-Saving... 23:11:37.875 - An error occurred 23:11:38.052 - Script 'Plugin_142298787.Multiplayer Studio.Script', Line 45 23:11:38.081 - Stack End 23:11:48.643 - DataModel Loading http://www.roblox.com/asset/?id=171180934 23:15:41.176 - Auto-Saving... 23:20:40.91 Ffbryce1 0 — 10y

1 answer

Log in to vote
0
Answered by
Shawnyg 4330 Trusted Badge of Merit Snack Break Moderation Voter Community Moderator
10 years ago

To start off, you have multiple while true do loops. When you run one of those loops, it'll keep running and never get past that loop unless you would use coroutine. Well, I have my way that may not be the most efficient, but always gets the job done. I'd make 3 scripts in total. One that would toggle the on/off, one that would be looping the on, and the last looping the off. Here's how it would look:

Toggle Script:

local on = false
OnScript = script.On -- Should be disabled by default
OffScript = script.Off -- Should be disabled by default

script.Parent.MouseButton1Down:connect(function() -- If this is a ClickDector, make it MouseClick
    if on == false then
        on = true
        OffScript.Disabled = true
        OnScript.Disabled = false
    else
        on = false
        OnScript.Disabled = true
        OffScript.Disabled = false
    end
end)

On Script: (Should be disabled by default)

Light1 = game.Workspace.Test4.Light1
Light2 = game.Workspace.Test4.Light2
Light3 = game.Workspace.Test4.Light3
Light4 = game.Workspace.Test4.Light4
Light5 = game.Workspace.Test4.Light5
Light6 = game.Workspace.Test4.Light6

while true do
    Light1.BrickColor = BrickColor.new("Medium stone grey")
    Light2.BrickColor = BrickColor.new("Really red")
    Light3.BrickColor = BrickColor.new("Medium stone grey")
    Light4.BrickColor = BrickColor.new("Really red")
    Light5.BrickColor = BrickColor.new("Really red")
    Light6.BrickColor = BrickColor.new("Really red")
    wait(1)
    Light1.BrickColor = BrickColor.new("Really red") 
    Light2.BrickColor = BrickColor.new("Medium stone grey")
    Light3.BrickColor = BrickColor.new("Really red")
    Light4.BrickColor = BrickColor.new("Medium stone grey")
    Light5.BrickColor = BrickColor.new("Medium stone grey")
    Light6.BrickColor = BrickColor.new("Medium stone grey")
    wait(1)
    Light1.BrickColor = BrickColor.new("Medium stone grey")
    Light2.BrickColor = BrickColor.new("Medium stone grey") 
    Light3.BrickColor = BrickColor.new("Medium stone grey")
    Light4.BrickColor = BrickColor.new("Medium stone grey") 
    Light5.BrickColor = BrickColor.new("Medium stone grey")
    Light6.BrickColor = BrickColor.new("Medium stone grey")
    wait(1)
end

Off script: (Should be disabled as default)

Light1 = game.Workspace.Test4.Light1
Light2 = game.Workspace.Test4.Light2
Light3 = game.Workspace.Test4.Light3
Light4 = game.Workspace.Test4.Light4
Light5 = game.Workspace.Test4.Light5
Light6 = game.Workspace.Test4.Light6

while wait() do
    Light1.BrickColor = BrickColor.new("Medium stone grey")
    Light2.BrickColor = BrickColor.new("Medium stone grey") 
    Light3.BrickColor = BrickColor.new("Medium stone grey")
    Light4.BrickColor = BrickColor.new("Medium stone grey") 
    Light5.BrickColor = BrickColor.new("Medium stone grey")
    Light6.BrickColor = BrickColor.new("Medium stone grey")
end
0
There is another way about approaching this, but I feel you'll understand this way better. Shawnyg 4330 — 10y
0
That did not work, I do understand(kind of). See this is a traffic director on a firetruck controlled by a screengui flipswitch. I flip it once and it is continuous loop until I flip it and it turns off. My probblem is getting it to turn off instantly not finish the script and then stop. Ffbryce1 0 — 10y
0
Are you sure this did not work? I've used this way a bunch of times and it always worked. Output? Shawnyg 4330 — 10y
Ad

Answer this question