How would I Check if an Textbox's Text was empty, I've tried this:
if tostring(Text) == nil then
print("Failed to Respond")
end
How would I Check if an Textbox's Text was empty, I've tried this:
if tostring(Text) == nil then
print("Failed to Respond")
end
There are multiple ways to go about doing this:
The string library gives a great way to determine the length of the text. It is the len method in the string library - short for length.
local text = "Hello, World!";
print(string.len(text))
All strings have a metatable attached to the original string library which means you can do this aswell:
local text = "Hello, World!";
print(text:len())
If you are familiar with the length operator, mainly seen as # in Lua, you realize that it gets you the length of arrays. Strings are technically arrays because they are arrays of characters.
Thus you can do:
local text = "Hello, World!";
print(#text)
You can also just check if your string equals an empty string.
local text = "";
assert(text ~= "", "Empty!")
More than likely there will be times where a user will just spam a bunch of spaces into your TextBox. We need to get rid of these spaces because Lua [and every other language] still counts spaces as a character length.
We can use string substitution.
local text = " ";
text = text:gsub("[%s]","") --Remove all whitespace
print("Empty? ", #text == 0)
String Pattern Complements: http://wiki.roblox.com/index.php?title=String_pattern#Complements
String Manipulation: http://wiki.roblox.com/index.php?title=Function_dump/String_manipulation#string.len
if textbox.Text=="" then
print("NOTHING ENTERED")
elseif textbox.Text==" "
print("NOTHING ENTERED")
else
print(textbox.Text)
end
Just use an if statement.
if text == "" then
print("Textbox is empty")
end
if text == "" then
This discussion is preserved as it was. New answers, comments, and votes are closed.
1 comment
That WAS my idea, but I was pretty sure there was a twist.