Need help with script

I need help with this simple button gui script. I would like it to change the value of a BoolValue but it is not working
Here is the on button

function OnClicked() 
	script.Parent.Parent.Parent.Parent.Body.Lights.On = true

end 

script.Parent.MouseButton1Down:connect(OnClicked)

And the off button

function OnClicked() 
	script.Parent.Parent.Parent.Parent.Body.Lights.On = false

end 

script.Parent.MouseButton1Down:connect(OnClicked)

if it’s a BoolValue you need to reference On.Value = true and not On = True as On is the instance itself and Value is the container for the instances’ type

3 Likes

Like @MP3Face said, you need to refrence On.Value = true not On = true

Maybe you don’t understand, copy this code:

function OnClicked() 
	script.Parent.Parent.Parent.Parent.Body.Lights.On.Value = true

end 

script.Parent.MouseButton1Down:connect(OnClicked)
function OnClicked() 
	script.Parent.Parent.Parent.Parent.Body.Lights.On.Value = false

end 

script.Parent.MouseButton1Down:connect(OnClicked)

This would result in something like this -

Value1 is not a valid member of TextButton "Players.Valkyrop.PlayerGui.ScreenGui.Frame.Buttons.1"

The reason, is simply because you’re trying to set an instance to a bool value, which you can’t. You always have to set the instance’s value ,and not the instance itself.

To solve this :

Simply add .Value after On.

function OnClicked() 
	script.Parent.Parent.Parent.Parent.Body.Lights.On.Value = true

end 

script.Parent.MouseButton1Down:connect(OnClicked)
function OnClicked() 
	script.Parent.Parent.Parent.Parent.Body.Lights.On.Value = false

end 

script.Parent.MouseButton1Down:connect(OnClicked)

There is also a shorter way to do all of that, which is simplier -

function OnClicked() 
    local var = script.Parent.Parent.Parent.Parent.Body.Lights.On
    var.Value = not var.Value

end 

script.Parent.MouseButton1Down:connect(OnClicked)

This line -
var.Value = not var.Value will simply turn it the ‘On’ value to false/true accordingly to its current status[meaning, if it’s true, it’ll set it to false, vice versa]

Use this short-cut if you’re using the same button.