Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

What would I use to make an auto completion in a string?

script.Parent.TextBox:GetPropertyChangedSignal("Text"):Connect(function()
		for _,v in pairs(game.Players:GetPlayers()) do
			if string.match(script.Parent.TextBox.Text, v.Name) then
				print(v)
		end
	end
end)

It only returns v if I write the full name, when I want it to match the first words in a string. How would I fix it so that it auto completes if I start writing a name?

2 answers

✓ Accepted answer

string.match

The first parameter is the string, the second is the pattern you are looking for. It should be the player's Name first and the TextBox.Text second.

string.match(plr.Name, TextBox.Text)

string.match will match anywhere in the string. string.match('helloPizza', 'Pizza') will return something. Use string.sub to stop this interaction.

string.lower, string.upper

string.match is case sensitive. By making the string lowercase/uppercase, it'll compare just the letters rather than checking if upper/lower letters are matching.

local lowerName = plr.Name:lower()
local lowerText = TextBox.Text:lower()

string.match(lowerName, lowerText)

Matching Nothing

Having the string be nothing "" will match with any player. You should check if the TextBox.Text is not equal to "" before running anything. Since you are autofilling, don't think this matters.

More

Since people could have similar names, you should not autofill until there is only one person that matches the string.

TextBox:GetPropertyChangedSignal("Text"):Connect(function()
	local matchedPlayers = {}
	
	for _,v in pairs(game.Players:GetPlayers()) do
		if string.match(v.Name:lower(), script.Parent.Text:lower()) then
			table.insert(matchedPlayers, v)
		end
	end
	
	if (#matchedPlayers == 1) then
		local autofill = matchedPlayers[1]
	end
end)

Good luck!

script.Parent.TextBox.Changed:Connect(function()
for i,v in pairs(game.Players:GetChildren()) do
if v.Name:match:(script.Parent.TextBox.Text:lower()) then
print(v)
end
end
end)

I believe this will work, it checks the players name when the text box text is changed and lowercases it. Wrote this on my phone so it might not be fully right (typos)

This discussion is preserved as it was. New answers, comments, and votes are closed.