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

TextLabel text dictated by the string inside a TextBox not working?

Asked by 4 years ago

I have a very basic 'search engine' sort of thing where inside a ScreenGUI on a model computer there is a textbox, textlabel, and textbutton.

When the button is clicked, the text in the textbox should dictate what the textlabel says.

However, for one, I can not type in the textbox, and the script just outputs what I told it to under a certain condition regardless of whether or not the text contained in the box fits that.

script.Parent.MouseButton1Click:connect(function()
    if script.Parent.Parent.string.Text == "keyword" or "keywords" then
        script.Parent.Parent.results.Text = "output"

    else

    end
end)

Does anyone know why I cant type in the textbox or why this script won't work?

1 answer

Log in to vote
0
Answered by 4 years ago

Remember that like control structures, logical operators (not, or, and) treat non-nil, non-false values as truthy. Nil and false are the only falsey values in Lua.

The issue is that since strings are not false or nil, they'll be treated as truthy in a condition context.

> "hello" or false
hello
> true or "hey"
true
> nil or "sup"
sup

The or operator evaluates its two operands, and gives you the first truthy operand.

If the first expression, script.Parent.Parent.string.Text == "keyword", evaluates to false, it will evaluate to "keywords" because the relational operator == has higher precedence.

Thus it would look kinda like this

if "keywords" then

end

Where "keywords" is truthy, since it's not false or nil.


How do we fix this?

Simply, compare the text directly again.

local button = script.Parent
local stringLabel = button.Parent.string
local results = button.Parent.results
-- makes code shorter

button.Activated:Connect(function()
    if stringLabel.Text == "keyword" or stringLabel.Text == "keywords" then
        results.Text = "output"
    end
    -- no else block needed
end)
0
Thank you! Octocentillion 15 — 4y
Ad

Answer this question