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

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

Asked by 10 years ago

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

Log in to vote
1
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
10 years ago

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.

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)? Devotional 210 — 10y
0
Yea, it would look something like that. BlueTaslem 18071 — 10y
0
Alrighty, thank you. You were great help! Devotional 210 — 10y
Ad
Log in to vote
1
Answered by
Muoshuu 580 Moderation Voter
10 years ago
local Text=" "

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

Answer this question