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

How do I get a working light that can be turned off and on?

Asked by 3 years ago
script.Parent.ClickDetector.MouseClick:Connect(function()
    script.Parent.Transparency = 0
    script.Parent.Material = "Neon"
    script.Parent.BrickColor = BrickColor.new("New Yeller")
    script.Parent.PointLight.Enabled = true
    wait(300)
    script.Parent.Transparency = 0.5
    script.Parent.Material = "Plastic"
    script.Parent.BrickColor = BrickColor.new("Dark stone grey")
    script.Parent.PointLight.Enabled = false 
end)

How can I make it so that I can click the light a second time to turn it off, without waiting 500 seconds? I'm new to scripting, just so you know.

1 answer

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

You can make a light var to check if it is on or off by using if statements! We check if the light var is false to turn it on and make it true. The else keyword will have a code inside when the var is true to be false and turn off the light.

local light = false
script.Parent.ClickDetector.MouseClick:Connect(function()
    if light == false then
            script.Parent.Transparency = 0
            script.Parent.Material = "Neon"
            script.Parent.BrickColor = BrickColor.new("New Yeller")
            script.Parent.PointLight.Enabled = true
        light = true
    else
            script.Parent.Transparency = 0.5
         script.Parent.Material = "Plastic"
            script.Parent.BrickColor = BrickColor.new("Dark stone grey")
            script.Parent.PointLight.Enabled = false 
        light = false
end
end)

Explanation:

The variable 'light' is currently set to false. Once the event fires, it checks if the light variable is true or false.

If true then, it will do:

script.Parent.Transparency = 0 -- Makes the transparency 0
script.Parent.Material = "Neon" -- Change Material to 'Neon'
script.Parent.BrickColor = BrickColor.new("New Yeller") -- 'Brick color 'New Yeller''
script.Parent.PointLight.Enabled = true -- PointLight property = true
light = true  -- The var will turn to true in order to turn it off when clicked again since the if statement checks if it is false or true

If false:

script.Parent.Transparency = 0.5 -- Makes the transparency 0.5
script.Parent.Material = "Plastic"-- Change Material to 'Plastic'
script.Parent.BrickColor = BrickColor.new("Dark stone grey")-- Brick color 'Dark stone grey'
script.Parent.PointLight.Enabled = false  -- PointLight property = false
light = false -- The var will turn to false in order to turn it on when clicked again since the if statement checks if it is false or true
0
You don't really need a var, you can just check if the light is enabled. VitroxVox 884 — 3y
0
Thank you! SmockyBocky 5 — 3y
0
@VitroxVox Yeah, just realised. AProgrammR 398 — 3y
0
On second thought, could you explain the script to me? SmockyBocky 5 — 3y
View all comments (2 more)
0
@SmockyBocky Edited the answer. Check the answer again. AProgrammR 398 — 3y
0
Thank you, I now understand :D SmockyBocky 5 — 3y
Ad

Answer this question