Get matching player names from aliases with UNKNOWN text in string

How would I get player with aliases and text in string?

--something i tried ;/
function findPlayers(txt)
	local plrs = Players:GetPlayers()
	local frame = script.Parent.Players
	
	for i,v in pairs(frame:GetChildren()) do
		if v.Name ~= "Player" then
			v:Destroy()
		end
	end
	
	for i,v in pairs(plrs) do
		if string.find(v.Name,txt) then
			frame.Visible=true
			local ex = frame.Player:Clone()
			ex.PlayerName.Text = v.Name
			ex.Name = v.Name
			ex.Visible = true
			ex.Parent = frame
			frame.Position = UDim2.new(0.117+TextBox.CursorPosition/1000, 0,0.798, 0)
		end
	end
	
end
3 Likes

You can just do

string.match(mainString, name), if it exists it will return, if not it will be nil. So you can just do

if string.match(mainString, name) then

If you’re getting the players from an Input made by the player, I recommend using :lower on both. So if the name is dDd then DDD, ddd, Ddd, dDD, etc will work, so it should look like
string.match(mainString:lower(), name:lower())

So, in your script is should look like this string.match(v.Name,txt), or string.match(v.Name:lower(),txt:lower())

1 Like

I need to get the player names aliases from a string. So imagine like “whats wrong with [xa]”.

1 Like

You don’t explain your issue well and then complain that we don’t get it right.
I have no idea what you’re trying to accomplish, maybe be a little more descriptive?

1 Like

Here’s an example:

Someone types in “hello how are you, [insert half of player’s name here]”
I need to get a possible matching player name from that part of the text.

1 Like

What do you mean half of the player’s name? Like the if the player’s name is 20 chars long, you want the first 10?

Edit: misread, sorry. I get what you want now

What you could try doing is using string. For example, my name is BIueMalibu. If someone typed “biue” into the chat, you could go through each player’s name one character at a time using a for loop to gauge the similarity between the alias and the player’s username.

There’s probably a string function to compare two strings, but I can’t remember it off the top of my head.

Edit 2: just realized that’s exactly what the other guy said lmao, my bad brother

1 Like

You can do string.split(input," ") to get all the words. Then loop through those words and check if any player matches.

It’s not the most efficient code, but I see no other way of doing this

local players = game.Players:GetPlayers()

local words = string.split(mainString," ")
for i,v in pairs (words) then
   for _,k in pairs (players) do
      if string.match(k.Name:lower(),v:lower()) then
         -- player found, watch out cause there might be more than 1 player available, you can add them to a table and check the table's length at the end
      end
   end
end
1 Like