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

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

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.

3 comments

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.

0

There is the lua logo, click that and put your code in there.

0

OK thanks, i see it now

3 answers

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.

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

3 comments

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.

0

Im sorry but i just started this website litterally 10 minutes ago so im not used to how it works :/

0

Oh nvm 16 minutes

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)

2 comments

0

Thank you so much i tested this and it worked perfectly!!!

0

Ninja'd

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