How to prevent an space from adding in the chat textbox when pressing tab?

I have an autocomplete system in place and when you press Tab it’d autocomplete to one of the matched words but it adds an space once it’s filled, how do you remove that space or make it so it doesn’t add entirely?

For example;

If I had a word in my autocomplete module called hello and I began to type the in chat then it’d display a TextButton beneath the Chat Bar displaying Hello and you can click it to autocomplete the message and it wouldn’t have any spaces and it’d just be Hello but when I press Tab then it fills the message in the TextBox for the Chat but it’d be Hello | but with the space in the place of the |.

So how would I prevent that from happening?

Here is the code;

if #matches > 0 then
			self.GuiObjects.SuggestionFrame.Size = UDim2.new(1,0,0,48)
			recommendationConnection = UserInputService.InputBegan:Connect(function(input, processed)
				if processed and input.KeyCode == Enum.KeyCode.Tab and self.TextBox.CursorPosition ~= -1 then
					recommendationPass = true
					local children = self.GuiObjects.SuggestionSubframe:GetChildren()
					table.sort(children, function(a,b) if a:IsA("UIListLayout") then return true elseif b:IsA("UIListLayout") then return false end return (a.Name:lower() < b.Name:lower()) end)
					recommendationIndex = (recommendationIndex) % (#children-1) + 2
					print(recommendationIndex)
					local addLen = 0
					if children[recommendationIndex-1]:IsA("TextButton") then
						children[recommendationIndex-1].TextTransparency = 0.4
						addLen = string.len(children[recommendationIndex-1].Name)
					end
					children[recommendationIndex].TextTransparency = 0
					for _, match in ipairs(matches) do
						if match[1] == children[recommendationIndex].Name then
							TextBox.Text = string.sub(TextBox.Text, 1, string.len(TextBox.Text) - string.len(match[2]))..match[1]
							TextBox.CursorPosition = string.len(TextBox.Text) + 1
							break
						end
					end
					recommendationPass = false
				end
			end)
		else
			self.GuiObjects.SuggestionFrame.Size = UDim2.new(1,0,0,0)
		end
2 Likes

Any ideas on what to do ? I’ve been trying to prevent this from happening for months.

2 Likes

Anything at all? Also I am using LegacyChatService, this script is located in ChatScript > ChatMain > ChatBar.

Can anybody help me with this, please.

The ‘space’ added is from the addition of a tab character, which is represented by the code \t. If you use gsub on the string, and replace \t with a blank string "", it should prevent the issue.

Can you provide more detail because I am confused.

Basically, you would just delete the character as soon as it’s added (or as soon as you process the autocomplete).
string.gsub() is the way to replace text in Roblox, with the parameters being string.gsub(TEXT, PATTERN, REPLACEMENT).

Can you provide where to put that line of code?

And would it be;

string.gsub(TextBox.Text, "\t", "")

If not that, then what would it be? I only need to remove that space after the last letter of the autocomplete text.

It would be

Textbox.Text = string.gsub(TextBox.Text, "\t", "")

I guess you could put it on line 5 of the provided code, after the if processed and input.... line.

1 Like

It didn’t work unfortunately, any other ideas?