New: Nitro Boost our Discord server and receive full donation perks here on the website! Join the Scripting Helpers Discord Server to learn more! You can also Support on Patreon as always.
Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

How can I make my button work?

Asked by 4 years ago

Hello, I am a newbie to Lua and I've been trying to fix this script for about a while and I can't figure it out. Basically I made a button that will make something transparent and turn on a point light when it's clicked, but I don't know how to turn the light off and restore the transparency when I click the button again, how would I do this? Here's my code.

function clicked(playerWhoClicked)
   Workspace.Shadows1.Transparency = 1
   Workspace.shadowlight.PointLight.Brightness = 5
end
script.Parent.ClickDetector.MouseClick:connect(clicked)

Thanks :)

1 answer

Log in to vote
1
Answered by
Redbullusa 1570 Moderation Voter
4 years ago

'if' and 'else' statements are wonderful things. It lets the program knows if a thing is in this condition, do these set of actions.

You can simply have your function laid out like this:

Boolean = true

function ExampleFunc()
    if Boolean == true then
        Boolean = false
    elseif Boolean == false then
        Boolean = true
    end
end

You can then shorthand the 'if'/'else' statement to save time and become more efficient (very important, and you will understand why).

if Boolean then
    Boolean = false
elseif not Boolean then
    Boolean = true
end

With not is equivalent to false or nil*. Consequently, the absence of not will make it true*.

*MISCONCEPTION: not actually means the opposite of a boolean value. Remember in math: if you multiply a negative number with another negative number, you will yield a positive number. So

not true == false

not false == true

Since the datatype we're manipulating is a boolean value, you can shorten the elseif statement into an else.

else
    Boolean = true
end

Because originally, the if statement says if the Boolean is true. Elsewise, it will be false.

To implement that knowledge into your script, you can have a static boolean variable and the set of ifs and elseifs.

On = false

-- Where your function starts

if On == false then
    On = true
    Workspace.Shadows1.Transparency = 0
    Workspace.shadowlight.PointLight.Enable = true -- You can preset your PointLight's brightness.
else
    On = false
    Workspace.Shadows1.Transparency = 1
    Workspace.shadowlight.PointLight.Enable = false
end
Ad

Answer this question