How do I script on/off buttons?

I have these 3 buttons and I’m confused on if I need ifelse statements in the code.

local Sunraysbutton = script.Parent
local Sunrays = game.Lighting.SunRays

script.Parent.MouseButton1Click:Connect(function()
	if Sunrays.Enabled == true then
		print("Sunrays are on.")
	else
		if Sunrays.Enabled == true then
	Sunrays.Enabled = false 
		print("Sunrays are off.")
	end 
end
2 Likes

do:

script.Parent.MouseButton1Click:Connect(function()
	if Sunrays.Enabled == true then
		print("Sunrays are on.")
	else
		
			Sunrays.Enabled = false 
			print("Sunrays are off.")
	end
	end)

else fires when the if statement is false. Dont do

if stuff == true then
else
if stuff == false then
end

. If you want something to fire if something is false AND a certain condition is true then use elseif. Hope this helped. If you have any questions then feel free to ask.

2 Likes

Also if you want the value to go on and off do:

script.Parent.MouseButton1Click:Connect(function()
Sunrays.Enabled = not Sunrays.Enabled
	end)

this script will change the enabled value to the opposite of what it currently is. For example, if the value is true it will become false. If it’s false then it will become true. If you have any questions then feel free to ask.

1 Like

Is the second part needed or is that extra?

the second part is just a simpler way from the first part. The first part was just telling you about else statements.

The sunrays will only stay on after the button is clicked.

you set it up wrong. Do this in a local script inside of the button. I wouldn’t have sent it you if it was wrong:
copy paste it to ensure u get nothing wrong.

script.Parent.MouseButton1Click:Connect(function()
	game.Lighting.SunRays.Enabled = not game.Lighting.SunRays.Enabled
end)

If you don’t feel like setting it up the use this place file. Feel free to use the scripts in ur game. If you have any questions then say below

On off button.rbxl (34.7 KB)

1 Like
script.Parent.MouseButton1Click:Connect(function()
	if Sunrays.Enabled then 
		print("Sunrays are on.")
	else
	    print("Sunrays are off.")
	end
end)

You don’t need “Sunrays.Enabled == true”.

I’m facing an issue were Color Correction is getting an error in code saying its not a valid member of Lightning.

Thats because the script runs before it loads in. Do:

local Correction = game.Lighting:WaitForChild("ColorCorrection")

Wait for child will wait until the item is there. Which is what we want when we want to wait for the item to load in.

I Put that in line 1 for the script and I’m still not able to turn off the color correction.

I’m also getting this error in the output.
StarterGui:SetCore must be called from a local script.

local Sunrays = game.Lighting:WaitForChild("SunRays")
local Button = script.Parent

Button.Parent.MouseButton1Click:Connect(function()
	if Sunrays.Enabled then
		Sunrays.Enabled = false
	elseif not Sunrays.Enabled then
		Sunrays.Enabled = true 
	end 
end)

You defined a reference for the button but didn’t use it, you forgot a closing bracket at the end of the function connected to the event, you’re checking if the sunrays are enabled but not turning them off if they are, you then check if the sunrays are enabled for a second time instead of checking if they are disabled, you should use elseif instead of else if as it keeps everything nearly inside a single conditional chain.

YOU DONT DO :SetCore. thats why

It says that MouseButton1click is not a valid member of Frame

local Sunrays = game.Lighting:WaitForChild("SunRays")
local Button = script.Parent

Button.MouseButton1Click:Connect(function()
	if Sunrays.Enabled then
		Sunrays.Enabled = false
	elseif not Sunrays.Enabled then
		Sunrays.Enabled = true 
	end 
end)

I apologise, I left a reference to the button’s parent in there mistake, I’ve removed that in the above.

Ok, Thanks! Could you also help with Bloom, Blur, and Color Correction those also seem not to work.