Hello,
I'm self-learning how to use a script that defines the function then uses it. I know some basics but I'm trying to perfect it. But I try this script, but it doesn't work.
local button = script.Parent local chosen = false function Chosen() if chosen == false then button.TextTransparency = 0 elseif chosen == true then button.TextTransparency = 1 end end button.MouseButton1Click:Connect(Chosen)
What i'm trying to do is basically when you press a text button, the button.TextTransparency = 0
, but that's if they haven't already chosen it. I'm making a choice selection GUI, so you pick a choice and then press a button to make the GUI disappear.
You press a text box that you have picked, and the text "Chosen" will come up. I have set the transparency the text to 1, so it can be set to zero. I then I have a variable called "local chosen = false" (basically meaning that it has not been chosen yet, but when it is chosen, the value will be set to true.
The issue is that once when I have pressed the option, it says "Chosen", and when I press it again, the text transparency is 1. This part is okay so far but the issue is when I press the button again, the text transparency does not change, but I want it to.
I'm guessing that some sort of loop might have to be set up to make sure this function occurs again and again.
How would I achieve this?
Thanks, Tony
The variable needs to be changed from false to true and vice versa for this function to work correctly.
local button = script.Parent local chosen = false function Chosen() if not chosen then button.TextTransparency = 0 chosen = true elseif chosen then button.TextTransparency = 1 chosen = false end end button.MouseButton1Click:Connect(Chosen)
this should help ~~~~~~~~~~~~~~~~~ local button = script.Parent local chosen = false
function Chosen()
if chosen == false then button.TextTransparency = 0 chosen=true--setting chosen to true-- elseif chosen == true then button.TextTransparency = 1 chosen=false--if you what to make it show when you click again-- end end
button.MouseButton1Click:Connect(Chosen)
~~~~~~~~~~~~~~~~~
You forgot to Update the variable after click.
Example:
-- Incorrect(NO UPDATE) local value = true script.Parent.MouseButton1Click:Connect(function() if value == true then print("True") -- Here repeat printing "True" in all clicks, why? you forgot to update the variable. for fix this, simple change the variable named as "value" to false. (see this in next example.) else print("False") end end) --[[ Output (3 clicks): True True True ]] -- Correct(UPDATING) local value = true script.Parent.MouseButton1Click:Connect(function() if value == true then print("True") value = false -- Updated the variable the variable for print "False" in next click else print("False") value = true -- Update the variable for print "True" in next click end end) --[[ Output (3 clicks): True False True ]]
Fixed script:
local button = script.Parent local chosen = false function Chosen() if chosen == false then button.TextTransparency = 0 chosen = true elseif chosen == true then button.TextTransparency = 1 chosen = false end end button.MouseButton1Click:Connect(Chosen)
Hope it helped, Errors? tell-me on comments