Auto-fill Caps Bug?

Hello Developers,

I currently have this auto-fill script and it works great and auto-fills but its not working with caps and does this:
image

Is there anyway to fix this?

Code:

local Players = game:GetService("Players")
local ContextActionService = game:GetService("ContextActionService")

local Gui = script.Parent

local PlayerName = script.Parent
local AutoFill = script.Parent.Parent.Autofill

local function GetPossiblePlayer(String)
	if String == "" then return nil end
	local PlayerList = Players:GetPlayers()
	for _, Player in pairs(PlayerList) do
		if Player.Name:sub(1, #String):lower() == String:lower() then
			return Player
		end
	end
	return nil
end

local function PlayerNameChanged(Property)
	if Property == "Text" then
		local PossiblePlayer = GetPossiblePlayer(PlayerName.Text)
		if PossiblePlayer then
			AutoFill.Visible = true
			AutoFill.Text = PossiblePlayer.Name
		else
			AutoFill.Visible = false
		end
	end
end

local function OnTabPressed(_, inputState)
	if inputState == Enum.UserInputState.Begin then
		local PossiblePlayer = GetPossiblePlayer(PlayerName.Text)
		if PossiblePlayer then
			PlayerName.Text = PossiblePlayer.Name:lower()
		end
	end
end

PlayerName.Changed:Connect(PlayerNameChanged)

ContextActionService:BindAction("OnTabPressed", OnTabPressed, false, Enum.KeyCode.B)

do you want to change the background text (AutoFill) or the text in the PlayerName TextBox ?

There is a text label behind it that is the autofill

If I understand correctly, you don’t want Caps to overlap No-caps?

Yes.
30chars30chars30chars30chars30chars30chars

AutoFill.Text = PlayerName.Text..(string.sub(possiblePlayer.Name,#PlayerName.Text+1))

will give you the right thing if i understand you problem

This will get rid of filled character, but that means you have to return the string entered in the GetPossiblePlayer() function:

AutoFill.Text = String..string.sub(PossiblePlayer.Name, #String + 1, #PossiblePlayer.Name)

or you could return the string.sub() in the GetPossiblePlayer() function.

string.sub takes a string as first parameter

1 Like

I am confused now- which one will work and which one wont-

i tested mine on lua demo, it should work fine i think. try both

Where would I put your script?

local function PlayerNameChanged(Property)
	if Property == "Text" then
		local PossiblePlayer = GetPossiblePlayer(PlayerName.Text)
		if PossiblePlayer then
			AutoFill.Visible = true
			-- put my scrpt here 
		else
			AutoFill.Visible = false
		end
	end
end

There is one problem tho : idk what you do with PlayerName.Text, but lets say that the name of the player is “Hello” and you type “hello”, if you do something like

Players:FindFirstChild(PlayerName.Text) -- which is equal to "hello"

it will not find the player since the ‘H’ is not upper case

You would have to do

local playerName = GetPossiblePlayer(PlayerName.Text).Name

and one last thing : instead of using

PlayerName.Changed:Connect(PlayerNameChanged)

and then verifying if the property == Text in the PlayerNameChanged function, you can use :

PlayerName:GetPropertyChangedSignal("Text"):Connect(PlayerNameChanged)

so you dont have to verify if the property == Text in the PlayerNameChanged function