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
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.
local Text=" " if not Text:match("%p") or not Text:match("%w") then print("Text has no real characters!") end