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

Why doesn't this SpotLight script work?

For some strange reason it is highlighting else as being red... Please help!

function onClick()
script.Parent.Parent.Parent.Lights.Lamp.SpotLight.Brightness=100
else
script.Parent.Parent.Parent.Lights.Lamp.SpotLight.Brightness=0
end

script.Parent.ClickDetector.MouseClick:connect(onClick)

1 answer

It is highlighted because there is no "If" statement.

Every "else" needs an "if" to go along with it.

I would help you finish the script, but I am unsure what you are trying to accomplish.

Example of if and else:

if x < 8 then
x = x + 5
else
x = x - 5
end

In order to get what you want done, try something like this...

function onClick()
--The following if statement will check the brightness of the spotlight
if script.Parent.Parent.Parent.Lights.Lamp.SpotLight.Brightness == 100 then
-- If the spotlight is already 100, then it will bring it down to 0
script.Parent.Parent.Parent.Lights.Lamp.SpotLight.Brightness=0
else -- If the spotlight is anything else, it will set it to 100
script.Parent.Parent.Parent.Lights.Lamp.SpotLight.Brightness=100
end
end

script.Parent.ClickDetector.MouseClick:connect(onClick)

2 comments

0

I want the spotlight to go off and on on the click of a button

0

In the edit, I added what I think would help. I have not tested it however. If you notice though, there is now an if and else statement that checks the birghtness of the spotlight and changes it accordingly. Also, don't forget to add an "end" after using these statements.

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