Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I not allow a message to have only spaces?

I made an in-game message GUI. However, I don't want it to be able to appear blank so how would I make it so the GUI checks if there's actually at least one letter in the textbox before everyone can see it?

The script goes something like this and it's a localscript underneath ScreenGui:

sp=script.Parent msg=workspace.GameMessage --string value msger=msg.Sender --also string value gui=sp.MsgGui textbox=gui.TextBox --where the message can be inputted

gui:WaitForChild("SendButton").MouseButton1Down:connect(function() if textbox.Text~=" " then --this is the part I'm talking about where I don't want it to send if it contains only spaces msg.Value=textbox.Text msger.Value=game.Players.LocalPlayers.Name else end

2 answers

✓ Accepted answer

Lua offers the %s character class for whitespace in its string processing functions.

We can strip out all whitespace using this character class and the gsub method.

withoutSpaces = str:gsub("%s","")
if #withoutSpaces > 0 then
	-- Still has content remaining
else
	-- No (non-space) content remaining
end

We could alternatively count the %S class, all non-whitespace characters.

3 comments

0

So would the script go something like this: gui:WaitForChild("SendButton").MouseButton1Down:connect(function() withoutSpaces=str:gsub("%s","") if #withoutSpaces>= then msg.Value=textbox.Text else end end)?

0

Yea, it would look something like that.

0

Alrighty, thank you. You were great help!

local Text=" "

if not Text:match("%p") or not Text:match("%w") then
	print("Text has no real characters!")
end

1 comment

0

"%S" is non-whitespace (or the more explicit "[^%s]"), so you could use that to reduce to one check. Alternatively "[%p%w]" would combine both classes.

This discussion is preserved as it was. New answers, comments, and votes are closed.