Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

What's wrong with my function script? The text transparency doesn't change after two clicks

Asked by 5 years ago

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

1
You need to update the variable chosen when you run the function, so it isn't always equal to false, add chosen = true after button.TextTransparency = 0 and add chosen = false after button.TextTransparency = 1 so when the function runs the next time, it does the opposite Sliam123456 1 — 5y

3 answers

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

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)
0
Thanks lol. I forgot to add the changing of the local variable in. IDK why but thanks. tonyv537 95 — 5y
Ad
Log in to vote
0
Answered by 5 years ago

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)

~~~~~~~~~~~~~~~~~

0
formatting issues but thanks. I got the gist. I missed out changing the value. tonyv537 95 — 5y
Log in to vote
0
Answered by
yHasteeD 1819 Moderation Voter
5 years ago
Edited 5 years ago

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


Solved your problems? Accept a answer or put [SOLVED] in title.
0
Yep I see, I'm dumb and just forgot to change the local variable I made. Thanks anyhow tonyv537 95 — 5y

Answer this question