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
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.
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.
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)
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.
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.