How would I make this script get a players name from only a small part of their name?

EmployeeMenu.GiveTool.MouseButton1Click:Connect(function()
	if SelectedTable[1] then
		for _,v in pairs(Players:GetChildren()) do
			if string.lower(v.Name) == string.lower(ToPlayer.Text) then--and string.lower(player.Name) ~= string.lower(ToPlayer.Text) then
				HandToEvent:FireServer(v.Name,SelectedTable)
				return
			end
			warn("Player not found")
		end
	else
		warn("You have not selected any items to Hand-To")
	end
end)

I am trying to make it so that, if you want, you can type in a small part of the players name and get the player. How would I do that?

2 Likes
if string.match(v.Name,ToPlayer.Text) then
--code

How would I add this into my current code? Sorry, I am not good at tables whatsoever.

No problem, I would simply add it on to your original if statement which checks strings.
It would look like:
if string.lower(v.Name) == string.lower(ToPlayer.Text) or string.match(v.Name,ToPlayer.Text) then

Do you know how I would make it so I could get a player from lowercase AND only part of their name? “and” is not working for me, but or does but it’s either full name no caps or part name with caps.

I did something like this for a script I was working on. This is a really simple function that should work perfectly, if I understood the question correctly.

Usage
Instance<Player> getPlayer(string n) -- Returns the first player with a username similar to the name provided.
local function getPlayer(n)
	for _, plr in pairs(game:GetService("Players"):GetChildren()) do
		if (string.lower(plr.Name):sub(0, #n)) then
			return plr
		end
	end
end
1 Like

Sure, it just involves adding more “string.lower()” into the script, nothing crazy. Just change “string.match(v.Name,ToPlayer.Text)” to “string.match(v.Name:lower(),ToPlayer.Text:lower())”

3 Likes