Is there a way to fix this?

Hello! I currently have a username “auto-fill” system. It usually works fine; as seen here.

image

Although, when a user’s name is capitalized, and a user types it lowercased, it looks off;

image

Is there an easy way to fix this?

Here is my code;

-- from my API script
function api.getPlayerWithNamePartial(Player)
	for _,Players in ipairs(Players:GetPlayers()) do
		if Players.Name:sub(1, #Player):lower() == Player:lower() then
			return Players
		end
	end
end

-- from my local script
local function checkPlayersForName(Name)
	for _,Players in pairs(Players:GetPlayers()) do
		if Players.Name:lower() == Name:lower() then
			return true
		end
	end
end

UserTextBox:GetPropertyChangedSignal('Text'):Connect(function()
	UsernameAutoComplete.Text = tostring(API.getPlayerWithNamePartial(tostring(UserTextBox.Text)))
	
	if string.len(UserTextBox.Text) == 0 then
		UsernameAutoComplete.Text = ''
	end
	
	if checkPlayersForName(UserTextBox.Text) == true then
		UsernameAutoComplete.Text = ''
	end
end)

UserTextBox.FocusLost:Connect(function()
	UsernameAutoComplete.Text = ''
end)

Thanks!

Maybe try to convert the inputted text to match the capitalization of the User’s name?

UserTextBox:GetPropertyChangedSignal('Text'):Connect(function()
	local autoFillName = tostring(API.getPlayerWithNamePartial(tostring(UserTextBox.Text)))
	UsernameAutoComplete.Text = autoFillName
	
	UserTextBox.Text = autoFillName:sub(1,#UserTextBox.Text)
	
	if string.len(UserTextBox.Text) == 0 then
		UsernameAutoComplete.Text = ''
	end
	
	if checkPlayersForName(UserTextBox.Text) == true then
		UsernameAutoComplete.Text = ''
	end
end)

It’ll get the name and store it in a variable, and set the text of the userTextBox to a string sub from the first letter to the length of the current text in the textbox. So you give it nic, it’ll give you Nic

@0V_ex can also work but it just depends on how you want to do it of course, if you want to keep the full autocomplete then use my method to fix the capitalizations, or use his method if you don’t really care about capitalization

2 Likes

Your LocalScript:

local function checkPlayersForName(Name)
	for _,Players in pairs(Players:GetPlayers()) do
		if Players.Name:lower() == Name:lower() then
			return true
		end
	end
end

UserTextBox:GetPropertyChangedSignal('Text'):Connect(function()
    local tbText = UserTextBox.Text
	local apiText = API.getPlayerWithNamePartial(tostring(tbText))
    UsernameAutoComplete.Text = tbText .. apiText:sub(#tbText+1, #apiText)
	
	if #tbText == 0 then
		UsernameAutoComplete.Text = ''
	end
	
	if checkPlayersForName(UserTextBox.Text) == true then
		UsernameAutoComplete.Text = ''
	end
end)

UserTextBox.FocusLost:Connect(function()
	UsernameAutoComplete.Text = ''
end)

Also, the code can be optimised in some ways, but I’m in a hurry :sweat_smile:

2 Likes