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

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

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

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

6 comments

0

You don't really need a var, you can just check if the light is enabled.

0

Thank you!

0

@VitroxVox Yeah, just realised.

0

On second thought, could you explain the script to me?

0

@SmockyBocky Edited the answer. Check the answer again.

0

Thank you, I now understand :D

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