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

The on/off script I created has some flaws . . Mind pointing them out? (Unsolved)

Asked by 10 years ago

I have 6 lights(1-6) in a group called "Test4" and that is in the workspace. The lights are labeled "Light1","Light2" and so on. I have a screengui button that when you sit down on the seat it equips, you click the button and the lights start to flash. You click the button again and they wait a second to finish the loop and then stop. I would like to make it so you click it to turn it on, but click it again and it immideatly turns off. The script I created it as follows:

On=false 
function onClicked() 
if On then On=false else 
On=true 
while On do 

    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("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(0.4)
    game.Workspace.Test4.Light1.BrickColor = BrickColor.new("Really red") 
    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("Medium stone grey")
    game.Workspace.Test4.Light5.BrickColor = BrickColor.new("Medium stone grey")
    game.Workspace.Test4.Light6.BrickColor = BrickColor.new("Medium stone grey") 
    wait(0.4)
    game.Workspace.Test4.Light1.BrickColor = BrickColor.new("Really red") 
    game.Workspace.Test4.Light2.BrickColor = BrickColor.new("Really red")  
    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(0.4)
    game.Workspace.Test4.Light1.BrickColor = BrickColor.new("Really red") 
    game.Workspace.Test4.Light2.BrickColor = BrickColor.new("Really red")  
    game.Workspace.Test4.Light3.BrickColor = BrickColor.new("Really red")
    game.Workspace.Test4.Light4.BrickColor = BrickColor.new("Really red")
    game.Workspace.Test4.Light5.BrickColor = BrickColor.new("Medium stone grey")
    game.Workspace.Test4.Light6.BrickColor = BrickColor.new("Medium stone grey")
    wait(0.4)
    game.Workspace.Test4.Light1.BrickColor = BrickColor.new("Really red") 
    game.Workspace.Test4.Light2.BrickColor = BrickColor.new("Really red")  
    game.Workspace.Test4.Light3.BrickColor = BrickColor.new("Really red")
    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("Medium stone grey")
    wait(0.4)
    game.Workspace.Test4.Light1.BrickColor = BrickColor.new("Really red") 
    game.Workspace.Test4.Light2.BrickColor = BrickColor.new("Really red")  
    game.Workspace.Test4.Light3.BrickColor = BrickColor.new("Really red")
    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(0.4)
    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(0)
end 
end
end

script.Parent.MouseButton1Down:connect(onClicked) 

The output is as follows: 11:33:07.922 - Use the new http api: no 11:33:21.960 - An error occurred 11:33:21.960 - Script 'Plugin_142298787.Multiplayer Studio.Script', Line 45 11:33:21.961 - Stack End 11:33:26.540 - DataModel Loading http://www.roblox.com/asset/?id=171180934 11:38:08.297 - Auto-Saving...

0
What is being clicked? A ClickDetector or a Surface Gui? EzraNehemiah_TF2 3552 — 10y

1 answer

Log in to vote
0
Answered by 10 years ago

There are several ways to improve this script, so let's go over them. First of all, when you don't specify a condition for an if statement, it will automatically assume that you are asking if the variable is not equal to nil (AKA if the variable exists). So in this script, you are asking in On is not equal to nil when you should be asking if On is true.

local On = false --Using local variables enables the server to retrieve variables faster
function onClicked() 
    if On == true then
        On = false
    else 
        On = true 
while On == true do

Second, your script is unnecessarily large. This can be fixed if you have knowledge of generic for loops. This might be a bit complicated, so I've tried to explain it the best way I can. Check out this article about generic for loops on Roblox Wiki if you're still confused.

for _, light in pairs(workspace.Test4:GetChildren()) do --This grabs a table of Test4's "children" (the lights) and runs the following block of code for each child
    if light.Name == "Light1" then --If the light is named Light1 then
        light.BrickColor = BrickColor.new("Really red") --The light's brickcolor will become red
    else --If the light is not named Light1 then
        light.BrickColor = BrickColor.new("Medium stone grey") --The light's brickcolor will become grey
wait(0.4)
for _, light in pairs(workspace.Test4:GetChildren()) do --Grabbing the table again
    if light.Name == "Light1" or light.Name == "Light2" --If the light is named Light1 or Light2 then
        light.BrickColor = BrickColor.new("Really red")
    else
        light.BrickColor = BrickColor.new("Medium stone grey")
wait(0.4)
--And so on...

Finally, your last wait seems unnecessary. The lowest wait time possible is 1/30th of a second, so you can just remove it if you don't need the wait.

If you found this answer helpful, you can upvote and accept it. If you have any questions, just ask. I hope this helps! :)

Ad

Answer this question