Issue with username auto complete script

So I’m trying to make it so the player can type in a players username partly, and auto complete with a button or Right Control. I have already made this work just fine, however for some reason it doesn’t work for multiple people.

The first person to join the server is the only one it works for.

Let’s say we had two people, and one of their names is “Jimmy”, and the other is “Johnny”. Jimmy can type Johnny’s username, but anybody else in the game is unable to be auto completed. Only Johnny’s. If you’re in a server alone, you can type your own username and it pops up in the auto complete box as it should, but for some reason you can’t when you’re in a server with 2+ players.

Hopefully that makes sense? Here is the code:

Text_Box:GetPropertyChangedSignal("Text"):Connect(function()

	if Text_Box.Text == "" then
		
		Hint.Text = "Start typing a username to auto complete."
		Hint.TextColor3 = Color3.fromRGB(255, 255, 255)
		Current_Auto_Name = ""
		Auto_Name.Text = ""
		
	elseif Text_Box.Text == Current_Auto_Name then
		
		Hint.Text = "Press the arrow icon to sync with '"..Current_Auto_Name.."'!"
		Hint.TextColor3 = Color3.fromRGB(133, 133, 255)
		Success:Play()
		
	else
		
		local Length_of_Text = string.len(Auto_Name.Text)

		for i, Player in ipairs(PlayerService:GetPlayers()) do
			
			if string.sub(Player.Name, 1, Length_of_Text) == Text_Box.Text then
				
				if Current_Auto_Name == Player.Name then
					Auto_Name.Text = Player.Name
					Current_Auto_Name = Player.Name
				else
					Auto_Name.Text = Player.Name
					Current_Auto_Name = Player.Name
					
					Hint.Text = "Press Right Ctrl or the Magic Wand to auto complete to '"..Current_Auto_Name.."'!"
					Hint.TextColor3 = Color3.fromRGB(0, 255, 0)
					
					Ping:Play()
				end

			else
				Hint.Text = "Unable to find a player to auto complete with."
				Hint.TextColor3 = Color3.fromRGB(255, 255, 255)
				Current_Auto_Name = ""
				Auto_Name.Text = ""
			end
			
		end
		
	end
end)

-- I'm only giving this segment of my script because that's the issue.
1 Like

bumping this because I still need help with it.

I don’t see anything blatantly wrong, have you tried adding prints to see what is going on under the hood? Usually a very good place to start. I would recommend adding:

print(Player, ":", string.sub(Player.Name, 1, Length_of_Text), " | ",Text_Box.Text)

This will give you the most likely causes. I don’t see why these wouldn’t work for multiple players, but it’s a good place to look.

I believe it’s because you didn’t break the for loop once it does find a player?
If it’s not that, then I believe the issue is lying somewhere else, as it didn’t have any problems with the test place I tried it on

Basically just add a break statement right after you find a matching player

-- ...
if string.sub(Player.Name, 1, Length_of_Text) == Text_Box.Text then
				
	if Current_Auto_Name == Player.Name then
		Auto_Name.Text = Player.Name
		Current_Auto_Name = Player.Name
	else
		Auto_Name.Text = Player.Name
		Current_Auto_Name = Player.Name
					
		Hint.Text = "Press Right Ctrl or the Magic Wand to auto complete to '"..Current_Auto_Name.."'!"
		Hint.TextColor3 = Color3.fromRGB(0, 255, 0)
					
		Ping:Play()
	end

	break -- Break the loop, as we've found the player we want already
else
-- ...
Code I tried it with

It’s the same code, just written differently.
I had also tried using your code, and it worked fine as well

local function FindPlayerWithNameContaining(Text: string): Player?
	Text = Text:lower()

	for i, Player in PlayerService:GetChildren() do
		local Name = Player.Name:lower()

		if string.sub(Name, 1, #Text) == Text then
			return Player
		end
	end
	
	return nil
end

Text_Box:GetPropertyChangedSignal("Text"):Connect(function()
	local Text = Text_Box.Text

	if Text == "" then
		Hint.Text = "Start typing a username to auto complete."
		Hint.TextColor3 = Color3.fromRGB(255, 255, 255)

		Current_Auto_Name = ""
		Auto_Name.Text = ""

		return
	end

	if Text == Current_Auto_Name then
		Hint.Text = "Press the arrow icon to sync with '"..Current_Auto_Name.."'!"
		Hint.TextColor3 = Color3.fromRGB(133, 133, 255)
		
		--Success:Play()
		
		return
	end
	
	local Player = FindPlayerWithNameContaining(Text)
	if Player then
		local Name = Player.Name

		Auto_Name.Text = Name
		Current_Auto_Name = Name
		
		if Current_Auto_Name ~= Name then
			Hint.Text = "Press Right Ctrl or the Magic Wand to auto complete to '"..Current_Auto_Name.."'!"
			Hint.TextColor3 = Color3.fromRGB(0, 255, 0)
			
			--Ping:Play()
		end
		
	else
		Hint.Text = "Unable to find a player to auto complete with."
		Hint.TextColor3 = Color3.fromRGB(255, 255, 255)
		Current_Auto_Name = ""
		Auto_Name.Text = ""
	end
end)
1 Like

I personally don’t think I explained my initial post very well, but it’s not the fact that my sync mechanic doesn’t fail, the auto complete text box can only auto complete to one username, and the rest fail.

Upon trying your break method, nothing happens at all now, in every new place I try.

It should pick the first instead of “Player” instead of trying to find 2 players with the same name. After adding the break statement, it no longer shows any available auto complete usernames, when it found 1 before.

(Doesn’t even detect that a fully typed username was inputted)

I forgot to tell you something else. My bad.
local Length_of_Text = string.len(Auto_Name.Text) should be local Length_of_Text = string.len(Text_Box.Text) instead.

Also remember to keep the break statement

1 Like

That solved the auto-complete issue! Only thing I noticed now is that for some reason it tells me that Player1 is fully typed, but when I go to type another user like Player2, instead of Player1, it only shows the green text with the “Press Right Control or the Magic Wand to auto-complete to USERNAME”.

Here is a video demonstrating what happens

Yeah, I noticed this.
It’s because of the order that your code checks things.
Basically you have to make it get an autocomplete name first THEN check if it matches, but your code does the reverse.

I rewrote it in a way so that it checks it in order:

Code
Text_Box:GetPropertyChangedSignal("Text"):Connect(function()

	if Text_Box.Text == "" then
		Hint.Text = "Start typing a username to auto complete."
		Hint.TextColor3 = Color3.fromRGB(255, 255, 255)
		Current_Auto_Name = ""
		Auto_Name.Text = ""
		
		return
	end
	
	if Text_Box.Text ~= Current_Auto_Name then
		local Length_of_Text = #Text_Box.Text

		for i, Player in PlayerService:GetChildren() do

			if string.sub(Player.Name, 1, Length_of_Text) == Text_Box.Text then

				if Current_Auto_Name == Player.Name then
					Auto_Name.Text = Player.Name
					Current_Auto_Name = Player.Name
				else
					Auto_Name.Text = Player.Name
					Current_Auto_Name = Player.Name

					Hint.Text = "Press Right Ctrl or the Magic Wand to auto complete to '"..Current_Auto_Name.."'!"
					Hint.TextColor3 = Color3.fromRGB(0, 255, 0)

					--Ping:Play()
				end

				break
			else
				Hint.Text = "Unable to find a player to auto complete with."
				Hint.TextColor3 = Color3.fromRGB(255, 255, 255)
				Current_Auto_Name = ""
				Auto_Name.Text = ""
			end

		end
	end

	if Text_Box.Text == Current_Auto_Name then

		Hint.Text = "Press the arrow icon to sync with '"..Current_Auto_Name.."'!"
		Hint.TextColor3 = Color3.fromRGB(133, 133, 255)
		--Success:Play()
	end
end)

Another thing is that, it won’t match for names if the autocomplete decides on someone else with continuation of it because of the index order (ex: if you write “username”, it may autocomplete someone called “username22” instead of someone called “username”).
To fix this, you’d have to remove the break statement and collect possible matches and pick the most likely

1 Like

Well the main issue I was looking to fix was the fact that it only worked for one person, but this seems to have fixed the issue. I don’t really care if someone has to type like 2 extra letters to get the result they wanted. I don’t expect this to be perfect, it’s really only meant for those long and niche usernames.

Thanks again, I appreciate the help.

1 Like