How to automatically guess the player name in which a player is typing? Read more to understand

The title says it all. A player is to type which player they want to give a gun to. Here is my current code which is case sensitive.
I want it to automatically “guess” which player they are typing and fill the text in while they are typing so that they would only often need to type a couple of letters, if that makes sense

local Player = game.Players.LocalPlayer
local Button = script.Parent
local PlayerName = Button.Parent.PlayerName

--> FUNCTIONS
Button.MouseButton1Click:Connect(function()
	local PlayerName = PlayerName.Text
	if game.Players:FindFirstChild(PlayerName) and PlayerName ~= Player.Name then
		game.ReplicatedStorage.MainUI_Events.GiveSpareGun:FireServer(game.Players[game.Workspace[PlayerName].Name])
		if game.Workspace[Player.Name]:FindFirstChild("ExtraGun") then
			game.Workspace[Player.Name].ExtraGun:Destroy()
		elseif game.Players[Player.Name].Backpack:FindFirstChild("ExtraGun") then
			game.Players[Player.Name].Backpack.ExtraGun:Destroy()
		end
		Button.Parent:Destroy()
	elseif PlayerName == Player.Name then
		Button.Text = "THE GUN ISN'T FOR YOU GREEDY PANTS!"
		wait(1)
		Button.Text = "GIVE!"
	else
		Button.Text = "INVALID PLAYER!"
		wait(1)
		Button.Text = "GIVE!"
	end
end)
local PlayerName = PlayerName.Text:lower()

I assume by “guess” you do not mean auto-completion but rather just ignore being case sensitive.
The code below gets the Players and lowercases the names when comparing ; if one lowercased string matches the other this that sets the Player’s Original name to the variable PlayerName.
Extra : note that from your code in PlayerName I assume it might refer to an Instance so I passed the --path as an argument to the tostring method.

local PlayerName =  tostring(--[[path]]) 
local Players = game.Players:GetChildren() 
for index , player in pairs(Players) do
    if(player.Name:lower() == PlayerName:lower()) then
	    PlayerName = player.Name
    end 
end 

That has helped, thanks, although I did mention auto completion too, how would I do that? If you’re willing to of course

local Players = game:GetService("Players")
local TextBox = script.Parent

TextBox.InputBegan:Connect(function(Input)
	if TextBox.Text:len() > 0 then
		local FoundPlayers = {}
		for _, Player in ipairs(Players:GetPlayers()) do
			if Player.Name:lower():match("^"..TextBox.Text) then
				table.insert(FoundPlayers, Player)
			end
		end

		if #FoundPlayers == 1 then
			TextBox.Text = FoundPlayers[1].Name
			TextBox:ReleaseFocus()
		end
	end
end)

This should be relatively simple to follow, the TextBox’s text autocompletes if the text uniquely matches the start of any player’s username. “#TextBox.Text > 0” can be changed to make it so that the search will only autocomplete once a certain number of characters has been entered.

Is there a way for it to autocomplete while the player is typing? And if the autocompletion is not the player they are typing, they can continue typing from where they left off?

I believe that is what it should do.

I used it and it takes some time for it to fill in, never while the payer is typing. Never mind anyway, I used a different method. Thanks for the help

Oh, I see the issue, InputBegan isn’t firing when the keyboard is pressed but it does fire when you move the mouse in/out of the bounds of the GuiObject. It isn’t a case of delay, just a weird nuance of “InputBegan” in relation to GuiObject instances.

local Players = game:GetService("Players")
local TextBox = script.Parent

TextBox:GetPropertyChangedSignal("Text"):Connect(function()
	if TextBox.Text:len() > 0 then
		local FoundPlayers = {}
		for _, Player in ipairs(Players:GetPlayers()) do
			if Player.Name:lower():match("^"..TextBox.Text) then
				table.insert(FoundPlayers, Player)
			end
		end

		if #FoundPlayers == 1 then
			TextBox.Text = FoundPlayers[1].Name
			TextBox:ReleaseFocus()
		end
	end
end)

I’ve just tested this and it should work as you intended.

1 Like