String.match not working

  1. What do you want to achieve?
    I’m trying to create a pseudo-code block with string.match and the RichText beta feature.

  2. What is the issue?
    The string.match function doesn’t work, and I know it’s something related to that and not the function since it works completely fine without that part.

  3. What solutions have you tried so far?
    I’ve looked all over the devhub, I’ve scoured the forums here, and still nothing. I tried asking other people in some Discord servers about it and they don’t have an answer either. I’ve tried making separate labels for that, but I figured it would take way too long and would be way too tedious. There aren’t any errors either.

Here’s the code:

local console = script.Parent.BackgroundConsole
local out = script.Parent.BackgroundOutput
local gui = script.Parent

console.Input.FocusLost:Connect(function()
	local temp = game:GetService("ReplicatedStorage"):WaitForChild("tempDefault"):Clone()
	
	local printM = string.match(console.Input.Text, "print")
	
	local text = console.Input.Text
	
	if printM then
		local TargetMessage = string.sub(text, 6)
		temp.Arrow.Font = Enum.Font.SourceSansItalic
		temp.Arrow.TextColor3 = Color3.fromRGB(117, 202, 255)
		temp.Arrow.Text = 'PRINT'
		temp.Message.Text = '("'.. TargetMessage.. '")'
	else
		temp.Parent = console.Holder
		temp.Message.Text = text
	end
end)

The problem in action:
https://gyazo.com/516bb40b67d552298cf1136815c60bad

It’s my first post here, so I’m sorry if I missed anything. :sweat_smile:

2 Likes

What debugging have you already done? Have you printed printM to see if it is nil?

Also, just a suggestion, look into regular expressions. You could do this instead with regular expressions.

console.Input.Text = “print test”
local printM = console.Input.Text:match(“^print\s+(.+)”)
print(printM) → test
It’s not much here, but you can do some pretty powerful expressions later on, if you want to make a console or command system it is almost essential.

There’s a phenomenal website called RegexOne that helped me learn it.

1 Like

Oh, here you go.
temp.Parent = console.Holder
You are missing this line in the ‘if’ section of the if-else.

2 Likes

Lua doesn’t support this. Your string.match pattern wouldn’t work, since Lua uses their own pattern syntax:

2 Likes

My bad, it’s been a while. It doesn’t matter much, but thanks for catching it.
console.Input.Text:match(“^print%s+(.+)”)

Thank you for your help! :smiley: I can’t believe I missed that. Silly me.