If statement not working

Hello,

So I wanted to create a Christmas Quiz to play when its Christmas but I ran into a issue which I can’t solve. Cause I wanted to put the player his answer in a TextBox. And then the Script is supposed to check if the answer is correct. But if I place an If statement and an else it only says the first scripted code.

And I have no clue why! It really would help if someone could help me with that.

Here’s the script if you need it.

wait(3)

local box = script.Parent.Parent.Parent.AnswerBox

local submit = script.Parent.Parent.Submit

submit.MouseButton1Click:Connect(function()
	if not box.Text == "How are you doing?" or "how are you doing" or "you doing?" or "you doing" then
		print("wrong")
		script.Parent.Parent.Parent.Parent.Wrong.Visible = true
		script.Parent.Parent.Parent.Parent.Ask_Roy.Visible = true



	elseif box.Text == "How are you doing?" or "how are you doing" or "you doing?" or "you doing" then
		print("wrong")
		print("its correct")
		local timer = script.Parent.Parent.Parent.Parent.Correct.TimerValue

		script.Parent.Parent.Parent.Parent.Correct.Visible = true
		script.Parent.Parent.Parent.Parent.Correct.Text = "Correct"

	end
end)
1 Like

Why not just do “if conditions true then” followed by an elder instead of the “if not” thing.

If statements do not work like that I’m afraid.

Using the or operator like that is not having the effect you think it is.

In lua, all values except for false and nil will evaluate to true when compared or evaluated.

For example:

local a = "Hello, world!"
local b = 57
local c = {}
local d = false
local e = nil

if a then print(a, "true") else print(a, "false") end
if b then print(b, "true") else print(b, "false") end
if c then print(c, "true") else print(c, "false") end

if d then print(d, "true") else print(d, "false") end
if e then print(e, "true") else print(e, "false") end

If you run this code you’ll see the output say something like:

Hello, world! true
57 true
table: 0xccf9e6acd7174e37 {} true
false false
nil false

As you can see, false and nil evaluate to false, everything else evaluates to true.

Right now, your if statement is saying “if box.Text is not equal to “How are you doing” or the string “how are you doing” evaluates to true, or the string “you doing?” evaluates to true, or the string “you doing” evaluates to true then”.

And since any string value will always evaluate to true, the if statement always passes.

I assume what you’re trying to do is something like, "If box.Text is not equal to “How are you doing?” and box.Text is not equal to “how are you doing” and box.Text is not equal to “you doing?” and box.Text is not equal to “you doing”.

In order to achieve this effect, you will have to write out each comparison explicity:

if box.Text ~= "How are you doing?" 
   and box.Text ~= "how are you doing" 
   and box.Text ~= "you doing?" 
   and box.Text ~= "you doing" 
then
    print("wrong")
end

As you can see it can be quite tedious to type all this out by hand, and if you ever want to change something it can take a lot of time to edit all the if statements. One way to get around this is by saving the pool of right answers in a table and checking if the input string is found in that table.

Here’s an example:

local Answers = {
   "How are you doing?",
   "how are you doing",
   "you doing?",
   "you doing"
}

local box = script.Parent.Parent.Parent.AnswerBox
local submit = script.Parent.Parent.Submit

submit.MouseButton1Click:Connect(function()
   if table.find(Answers, box.Text) then 
      -- we found the input string in the Answers table so it is a valid answer.
   else
      -- we didn't find the input string in the Answers table so they got it wrong!
   end
end)

The table.find function searches the table provided for the value (in this case, your input string) and returns a result if it found a match, otherwise it returns nil, which, if you remember, evaluates to false. Therefore, it is an easy way to check if your input value matches any value in a set.

I hope this makes sense, and if it doesn’t or you have any questions, please ask!

3 Likes

I really liked this example, of you used those metatables. I’ve tried using metatables for this too but it didn’t work and now I know why cause I forgot the commas. Anyways I’ve tried this script, but if I run it and no matter what answer I put in if its a correct answer or not. It still says that the answer is correct.

Can you show me your code? I just tested it and it works fine for me.

local input = "ahjdaluhd" 

local Answers = {
   "How are you doing?",
   "how are you doing",
   "you doing?",
   "you doing"
}
if table.find(Answers, input) then 
   print("found") 
else 
   print("nay") 
end

If you run this in the console it should print “nay”.

wait(3)

local box = script.Parent.Parent.Parent.AnswerBox

local submit = script.Parent.Parent.Submit

local Answers = {
	"How are you doing?",
	"how are you doing",
	"you doing?",
	"you doing"
}

submit.MouseButton1Click:Connect(function()
	if table.find(Answers, box.Text) then
		
		
		script.Parent.Parent.Parent.Parent.Wrong.Visible = true
		script.Parent.Parent.Parent.Parent.Ask_Roy.Visible = true



	else 
		print("wrong")
		print("its correct")
		local timer = script.Parent.Parent.Parent.Parent.Correct.TimerValue

		script.Parent.Parent.Parent.Parent.Correct.Visible = true
		script.Parent.Parent.Parent.Parent.Correct.Text = "Correct"

	end
end)

I know that if you do have the correct answer it will say its wrong cause I typed the wrong code in the wrong box. But even then it should say it. But than in the opposite way.

Just before the if statement put “print(box.text)” and see what’s being printed. If it’s the string then you know it’s a coding issue.

Actually the code works. When the answer is correct your having “wrong” and “ask Roy” set to true. You just need to change what your setting as true.

I finally know what the issue is but I don’t know how I can solve it so if you guys can help me with that, that would be awesome!

The issue is when I say if table.find(Answers, box.Text) then but somehow that is not true. And I have no clue how I can set that to true.

What does your code look like now?

Your code seems to be working in the video you sent, you just need to swap what is under the if and else conditional here

This is my code:

wait(3)

local box = script.Parent.Parent.Parent.AnswerBox

local submit = script.Parent.Parent.Submit

local Answers = {
	"How are you doing?",
	"how are you doing",
	"you doing?",
	"you doing"
}

submit.MouseButton1Click:Connect(function()
	print("YIPPE")
	if table.find(Answers, box.Text) then
		print("YIPPE2347329478")
		print("wrong")
		print("its correct")
		local timer = script.Parent.Parent.Parent.Parent.Correct.TimerValue

		script.Parent.Parent.Parent.Parent.Correct.Visible = true
		script.Parent.Parent.Parent.Parent.Correct.Text = "Correct"
		



	else 
		
		print("DUFAJDFJKAL")
		script.Parent.Parent.Parent.Parent.Wrong.Visible = true
		script.Parent.Parent.Parent.Parent.Ask_Roy.Visible = true

	end
end)

I don’t see an issue with your code, can you send another video.

This is the video with the current script I send before

You should be using a LocalScript

1 Like

If statements don’t work like that, if you put the “or” operator, you need to put the property again. Also, instead of checking loads of possible inputs, you can just put

if not string.lower(box.Text) == "how are you doing?" then