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

My if statement wont register even though the statement is true?

Asked by 5 years ago
Edited by User#19524 5 years ago
Afk = false

local Button = script.Parent

function onClick()
print("Clicked")
Afk = not Afk
print(Afk)
end

if Afk == true then
        Button.Text = "Afk is On"
    print("Afk is on")
    end

if Afk == false then
        Button.Text = "Afk is Off"
    print("Afk is off")
    end

Button.MouseButton1Click:connect(onClick)

This is my code, what its supposed to do is when the Gui button is clicked is toggles the value Afk to its opposite and then prints what the current status of Afk is, and depending on what the value is it will change the text of a gui button and print whether afk is on or not, but when i click the button it shows the status and yes it does print it toggling back and forth, so i know the value is changing, but the if statements arent running. I would really appreciate if someone could help me figure out why.


Edit: I put your code in a codeblock. It is expected that you do this yourself next time. How to format questions/answers on the main site.
0
And also i am new to this website so i dont know how to make the scripts in here look like a script block. Voidyll 8 — 5y
0
There is the lua logo, click that and put your code in there. Rheines 661 — 5y
0
OK thanks, i see it now Voidyll 8 — 5y

3 answers

Log in to vote
1
Answered by 5 years ago

The issue is that your if statements are outside of your function. Meaning the if statements will only execute once. You should move them inside of your function so that they will execute along with the other code.

local Afk = false
local Button = script.Parent

local function onClick()
    print("Clicked")
    Afk = not Afk
    print(Afk)

    if Afk then -- == true not needed
            Button.Text = "Afk is On"
            print("Afk is on")
    else: -- if Afk is FALSE
            Button.Text = "Afk is Off"
            print("Afk is off")
        end
end

Button.Activated:Connect(onClick)

It is also recommended that you localise your variables and indent your code for the sake of readability. You should also use Activated rather than MouseButton1Click since according to this post by a staff member, the latter will eventually be deprecated, and finally, :connect() is deprecated, use :Connect()/


Hopefully this answered your question, and if it did, then don't forget to hit that "Accept Answer" button. If you have any other questions, then feel free to leave them down in the comments.
Ad
Log in to vote
0
Answered by 5 years ago
Afk = false

local Button = script.Parent

function onClick()
print("Clicked")
Afk = not Afk
print(Afk)
end

if Afk == true then
        Button.Text = "Afk is On"
    print("Afk is on")
    end

if Afk == false then
        Button.Text = "Afk is Off"
    print("Afl is off")
    end

Button.MouseButton1Click:connect(onClick)

here is the code in the correct format hopefully

0
Scripting Helpers has a preview button that you should click when asking/answering questions. Also please do not post the correctly formatted code as an answer. I already formatted it. User#24403 69 — 5y
0
Im sorry but i just started this website litterally 10 minutes ago so im not used to how it works :/ Voidyll 8 — 5y
0
Oh nvm 16 minutes Voidyll 8 — 5y
Log in to vote
0
Answered by
Rheines 661 Moderation Voter
5 years ago
Edited 5 years ago

Your text doesn't change because it is not put inside the clicked event. It is going to run the check once, and that is at the start of the game. You just need to check for the AFK value inside the clicked event. Also, it's recommended to use local variables as they can be accessed faster.

--Use local variables.
local Afk = false

local Button = script.Parent

function onClick()
    print("Clicked")
    Afk = not Afk
    print(Afk)

    --check for AFK value inside onClick
    if Afk == true then
        Button.Text = "Afk is On"
        print("Afk is on")
    else
        Button.Text = "Afk is Off"
    end
end

--:connect is deprecated, use :Connect
Button.MouseButton1Click:Connect(onClick)
0
Thank you so much i tested this and it worked perfectly!!! Voidyll 8 — 5y
0
Ninja'd User#24403 69 — 5y

Answer this question