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!