Hello, I am making a Trivia Game, and I want the user to type the correct input inside the Textbox. I want user input to be correct so I have created a script to verify user input. I want the script to ignore if a user accidentally adds a spacebar in front of or behind the text.
local function Question_Answer(Answer)
local Input = game.Players.LocalPlayer.PlayerGui.ScreenGui.ScrollingFrame.Screen.Input
local Submit = game.Players.LocalPlayer.PlayerGui.ScreenGui.ScrollingFrame.Screen.Submit
Submit.MouseButton1Click:Connect(function()
local Input_Lower = string.lower(Input.Text)
if Input_Lower == Answer then
game.Players.LocalPlayer.PlayerGui.ScreenGui.ScrollingFrame.Screen.Correct.Visible = true
else
game.Players.LocalPlayer.PlayerGui.ScreenGui.ScrollingFrame.Screen.Wrong.Visible = true
end
end)
end
Question_Answer('yes')
You could use gmatch to capture the non-whitespace text.
local Message = "\t\tHello\t\nWorld!\t\t" -- \t is tab, \n is newline lol
local Words = {} -- Contains the words
for Word in Message:gmatch("%S+") do -- The `%s` pattern is used to capture text that's whitespace, i.e. tabs, newlines and even spaces. `%S` tells the program to capture anything that is not whitespace and return it.
Words[1+#Words] = Word -- Adds non-whitespace to the Words array
end
local Answer = table.concat(Words, " ") -- Pieces them together
print(Answer) -- Hello World!