How can I make this work when there is a string or word/letter infront of it?

I am trying to make an autofill cmd bar for my upcoming admin, but the autofill I am making only suggets the player name when there is nothing infront of the name. (e.g | Treeman34, If you type tr or t or any thing in the name it will suggest the name but if theres something infront it will not.)

Script is below, thanks!

Input:GetPropertyChangedSignal("Text"):Connect(function()
	while Input.Text:sub(1, 1):match("[^%a]+") do 
		Input.Text = Input.Text:sub(2)
	end
	if Input.Text:match("\t") and Auto.Text ~= "" then
		Input.Text	= Auto.Text .. "\32"
		Input.CursorPosition = #Input.Text + 1
		return
	end
	
	local text = string.lower(Input.Text)
	local Results = {}
	local Players = game:GetService('Players'):GetPlayers()

	for _, a in ipairs(Players) do
		if string.find(string.sub(a.Name:lower(), 1,string.len(Input.Text)), Input.Text:lower()) then
			table.insert(Results,a.Name)
		end
	end
	
	if text ~= "" then
		for _, cmd in ipairs(Results) do
			for _, name in ipairs(Results) do
					Input.Text = text
					if string.find(string.sub(name:lower(), 1,string.len(Input.Text)), Input.Text:lower()) then 
						Auto.Text = name:lower()
						return
					end
				end
		end
		Input.Parent.Autofill.Text = ""
	else
		Input.Parent.Autofill.Text = ""
	end
end)

If I understand what you’re asking correctly, all you’d need to do is to repeat this process after removing the last character in your string. E.g. if “tri” doesn’t have a result, go back and try with “tr” instead, finding “treeman”.

(e.g | Treeman34, If you type tr or t or any thing in the name it will suggest the name but if theres something infront it will not.)

I’m trying to make an autofill (AutoComplete) textbox / CMDBar.
If there is a player named lets say Joe43 and you type for example, kill Jo and Joe43 is in the server then it will pop up like an autocomplete on a google doc having the rest of the name, Joe43. I’ll send a video if you still don’t understand

I don’t understand what you mean by “if theres something infront”. Like an invalid character?

Sorry, I meant like anything like a command so the string infront is Kill

Heres a picture of the autofill.
image
the gray text is the autofill, I typed P (Poopypants is the decoy lol sorry for the name)

Well, if you know that the command is “Kill”, then you can remove that from the text being searched. It’s probably trying to look for a player named “Kill Treeman34” while you’d want to cut out "Kill ".

Well you don’t know the command, It’s a command bar so you can’t just whitelist “Kill” and nothing else

Then just split up the text with spaces using string.split(text, " "). The first item will be the command, and the second or more will be the arguments. You can then perform your autofill on string.split(text, " ")[2].

You can always just check for spacebars assuming this is a standard command system

Theres 2 textboxes, One for the input and the other for the autocomplete. Would split work?

I’m just saying that you can use split to split up the query into the command and arguments. You can then use autofill on the command or the arguments the same way that you do right now.

How would I exactly find the name, im confused because as of rn I’m using

if string.find(string.sub(name:lower(), 1,string.len(Input.Text)), Input.Text:lower()) then
Autofill.Text = name:lower()
end

How could I add split there

You would get the split up query and then perform the autofill on the last item. So if you were typing “kill treem”, it would perform autofill on “treem”, since it’s the last item in the split up table. Here’s a neat function that returns the autofilled value:

local function autofill(query: string, options: {string}): string
	if query == "" then return "" end

	local split = string.split(query, " ")
	local start = string.gsub(query, split[#split], "")
	
	query = split[#split]

	local length = string.len(query)

	for i = 1, #options do
		local loption = options[i]:lower()
		
		if string.find(string.sub(loption, 1, length), query:lower()) then 
			return start .. loption
		end
	end

	return ""
end

options is a table of strings which are the autofill options (like the player names).

If you’re confused, here’s an example:

print(autofill("kill treem", {"Treeman34", "L0RD_Scriptz"})
-- Will print "treeman34"
print(autofill("ki", {"kill", "ban"})
-- Will print "kill"

Can you give me an example on how you would call the function?

Something like

Input:GetPropertyChangedSignal("Text"):Connect(function()
	autofill(args)
end)

See the edit for an example. In your current code, you would use it like this:

-- Remove the entire if text ~= "" then block
Input.Parent.Autofill.Text = autofill(text, Results) -- You can set text to Input.Text instead of string.lower(Input.Text)

Also, you can simplify the first for loop (where you insert player names into Results) to:

for i = 1, #Players do
  table.insert(Results, Players[i].Name)
end

As a side note, I would rename Results to playerNames. Of course, you will also have to expand this system should you want autofill for commands as well. Right now, it’s very primitive.

It does indeed work but now the autofill text stays in the same spot. By any chance would you know how to make it behind the command. (e.g | kill p(autofill)

image

and it just suggests the username without me typing P or the rest of the name