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

Is This Script Right?

Asked by 10 years ago

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)
0
So do you want us to check your script or...? Octillerysnacker 115 — 10y
0
Yes - Why Do You Ask? (If There Is Any Errors) TheThomasBuilder 0 — 10y
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). Redbullusa 1580 — 10y
0
kthx Red TheThomasBuilder 0 — 10y

1 answer

Log in to vote
0
Answered by
Redbullusa 1580 Moderation Voter
10 years ago

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)
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) TheThomasBuilder 0 — 10y
Ad

Answer this question