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

Is there something wrong in this Boolean script?

Asked by 9 years ago

Is there something wrong with this script? Studio crashes every time I run a test with this script I made:

e1 = script.Parent.EmergencyLight1
e2 = script.Parent.EmergencyLight2
e3 = script.Parent.EmergencyLight3
e4 = script.Parent.EmergencyLight4
e5 = script.Parent.EmergencyLight5
es = script.Parent.EmergencyLightSc
val = script.Parent.Value.Value

while true do
    if val == true then 
        e1.SpotLight.Enabled = true
        e1.SpotLight2.Enabled = true
        e2.SpotLight.Enabled = true
        e2.SpotLight2.Enabled = true
        e3.SpotLight.Enabled = true
        e3.SpotLight2.Enabled = true
        e4.SpotLight.Enabled = true
        e4.SpotLight2.Enabled = true
        e5.SpotLight.Enabled = true
        e5.SpotLight2.Enabled = true
        es.SpotLight.Enabled = true
        es.SpotLight2.Enabled = true
        es.SurfaceGui.TextLabel.Visible = true
    else
        e1.SpotLight.Enabled = false
        e1.SpotLight2.Enabled = false
        e2.SpotLight.Enabled = false
        e2.SpotLight2.Enabled = false
        e3.SpotLight.Enabled = false
        e3.SpotLight2.Enabled = false
        e4.SpotLight.Enabled = false
        e4.SpotLight2.Enabled = false
        e5.SpotLight.Enabled = false
        e5.SpotLight2.Enabled = false
        es.SpotLight.Enabled = false
        es.SpotLight2.Enabled = false
        es.SurfaceGui.TextLabel.Visible = false
        end
end

2 answers

Log in to vote
4
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
9 years ago

duckwit is correct: infinite loops with no waits crash ROBLOX. You also have to reference the ValueObject, not its current Value.

Additionally, there are two things that make this a lot smaller and more efficient:

Since you only want to update the values when the Value changes, you can use the Changed Event of ValueObjects. If you'll notice, when the val is true, you set everything to true, and vice versa. You can simply set everything to the value of val for ease:

e1 = script.Parent.EmergencyLight1
e2 = script.Parent.EmergencyLight2
e3 = script.Parent.EmergencyLight3
e4 = script.Parent.EmergencyLight4
e5 = script.Parent.EmergencyLight5
es = script.Parent.EmergencyLightSc
val = script.Parent.Value

val.Changed:connect(function()
    e1.SpotLight.Enabled = val.Value
    e1.SpotLight2.Enabled = val.Value
    e2.SpotLight.Enabled = val.Value
    e2.SpotLight2.Enabled = val.Value
    e3.SpotLight.Enabled = val.Value
    e3.SpotLight2.Enabled = val.Value
    e4.SpotLight.Enabled = val.Value
    e4.SpotLight2.Enabled = val.Value
    e5.SpotLight.Enabled = val.Value
    e.SpotLight2.Enabled = val.Value
    es.SpotLight.Enabled = val.Value
    es.SpotLight2.Enabled = val.Value
    es.SurfaceGui.TextLabel.Visible = val.Value
end)
Ad
Log in to vote
3
Answered by
duckwit 1404 Moderation Voter
9 years ago

Why does Studio crash?

The reason that Studio crashes is because you have, by what you have written, instructed the computer to endlessly devote all of its resources to checking the truth of a boolean value and then setting the properties of various lights.

while true do
--Your code here
end

That is the purpose of a while loop, to continue checking the condition and then executing the contents until either the computer fails, the boolean condition is broken, or until the loop yields.

You want the loop to yield - that means to surrender its current processes for a time interval, allowing the computer to handle different tasks instead of this one loop.

The most straightforward way to yield any process is to make a call to the wait() function. You should have the loop wait() a little bit each time through the loop, for example:

while true do
    wait()
    if val == true then
        --Do stuff with your lights
    end
end

You should see the description of wait() on the Official ROBLOX Wiki and also read this blog article about using wait()

According to the Lua 5.1 Reference Manual, anything that is not false or nil will evaluate to true in a boolean expression, and because of that, many people use this 'shorthand' notation when writing while loops:

while wait() do
    if val == true then
        --do stuff with your lights
    end
end

Value Instances

We'd need to know more about what script.Parent.Value.Value is to properly answer this, but I'm guessing that it's referring to the Value property of an Instance that is a child of script.Parent and is called "Value". If this is the case, I recommend naming it something more descriptive, for clarity, such as "LightSwitch" or "EmergencyToggle". That line would then become:

val = script.Parent.LightSwitch.Value

There is another slight problem here, though. The line above stores the current boolean value of the LightSwitch BoolValue Instance in a variable, it does not keep a reference to the Instance itself. What this means is that when LightSwitch.Value changes, val will not change. To avoid this problem, you'll want to store a reference to the BoolValue Instance itself:

switch = script.Parent.LightSwitch

Then in your loop, check the Value property of switch like-so:

if switch.Value == true then
    --Do things
end

Tip

switch.Value is a boolean value; it has a value of either true or false. Therefore, for ease of writing, you don't need to use the expression if switch.Value == true, instead you can just write if switch.Value.

Putting it all together, you now have this loop to work with:

switch = script.Parent.LightSwitch

while wait() do
    if switch.Value then
        --Lights on
    else
        --Lights off
    end
end

Make sure to read the blog article about using wait() Edit: The ultimate condition should be switch.Value, not just switch

1
You have a typo in your final code block. Perci1 4988 — 9y
0
Thanks! duckwit 1404 — 9y

Answer this question