Can someone help me to indentify spaces in a text box

So i’ve made a text box that detects if there’s text or not.

local gui = script.Parent
local TextBox = gui.TextBox
local TextLabel = gui.TextLabel
local ImageLabel = gui.Icon

TextBox.Changed:Connect(function()
	if TextBox.Text:len() == 0 then
		TextLabel.Visible = true
		ImageLabel.Visible = true
	else
		TextLabel.Visible = false
		ImageLabel.Visible = false
	end
end)

And i want it to detect if there are any spaces so it wont falsely trigger or something, if someone could help i would be very thankful.

1 Like

Make it textbox.FocusLost instead

If i do it then this function will not happen like how it should be.

i think you should try using string.find(text," ")

This should work right?

local gui = script.Parent
local TextBox = gui.TextBox
local TextLabel = gui.TextLabel
local ImageLabel = gui.Icon

TextBox.Changed:Connect(function()
	if string.find('%p+', '') then
		if TextBox.Text:len() == 0 then
			TextLabel.Visible = true
			ImageLabel.Visible = true
		else
			TextLabel.Visible = false
			ImageLabel.Visible = false
		end
	end
end)
1 Like

video of it working:

1 Like
local gui = script.Parent
local TextBox = gui.TextBox
local TextLabel = gui.TextLabel
local ImageLabel = gui.Icon

TextBox.Changed:Connect(function()
	if TextBox.Text:len() == 0 or TextBox.Text:match("^%s+$") then --if length of textbox text is 0 or if textbox text only contains whitespace characters
		TextLabel.Visible = true
		ImageLabel.Visible = true
	else
		TextLabel.Visible = false
		ImageLabel.Visible = false
	end
end)

Not entirely sure what you’re trying to achieve but if you want to match a string which only contains whitespace characters then you would do the following.

TextBox.Text:match("^%s+$")