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

Is This Script Right?

I Wanted To Make A Light Switch With These Items: UnionOperation ("BackLamp") And The Children --> ClickDetector ("ClickSwitch") / PointLight ("Light") / A Script (Below)

local LightShine = script.Parent.Light.Enabled

function onMouseClick()
	if LightShine == true then
		LightShine = false
	else
		LightShine = true --IDK How To Switch It Back To "On" :(
	end	
end

script.Parent.ClickSwitch.MouseClick:connect(onMouseClick)

4 comments

0

So do you want us to check your script or...?

0

Yes - Why Do You Ask? (If There Is Any Errors)

0

Please don't start your topic as "Is This Script Right?" That is way to broad, and other curious people might not be able to get an answer and might end up having to ask the same problem as you do. I suggest titling it something like "'if' statement won't turn light back on" (I think that's a bad topic statement, but it's a start).

0

kthx Red

1 answer

One of the most common problems while beginners script is setting a variable to the property. I was once the victim of this mistake... or twice.

Anyways, if you set a variable to a property, you only will return the value of the property, not the location.

local LightShine = script.Parent.Light.Enabled
-- YOU: I want to set a variable to this property, because it simplifies everything.
-- CODE: I want to receive the value of this property!
-- YOU: *codes*
print(LightShine)
-- OUTPUT:
-- > false
-- YOU: Wait, what?

^That's how it basically goes. It will return the value of that boolean property. Same goes for any other property.

To fix this, just set the variable to the object.

local LightShine = script.Parent.Light

function onMouseClick()
	if LightShine.Enabled == true then
		LightShine.Enabled = false
	else
		LightShine.Enabled = true
	end 
end
script.Parent.ClickSwitch.MouseClick:connect(onMouseClick)

1 comment

0

umm... I already tried that and failed. Maybe I'll try it again to see my light switch active! (Maybe if TheThomasBuilder.haveTime == true then)

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