How to find an Instance using parts of a string?

I’ve been having some trouble creating an auto-find or auto-complete system for one of my game’s chat commands.

I want to have it so that when a player says /pay s the script should go through a folder I’ve set in Replicated storage and find the first instance which has a name starting with S.

I understand how to make chat commands and I’ve already set-up the scripts for it.
Here’s what I’ve currently got:
(The command I’d like to manipulate with an auto-finder/auto-complete is the one which states if Part[1] == /pay)
image_2021-07-31_194746

And here’s the location of the list of players.
image_2021-07-31_194842

ALSO: If anyone has any help regarding deleting messages sent by players, I’d be glad if it was mentioned too!

Thank you in advance…

1 Like

What if you check if /pay is in the string in lua that would be

if Part[1]:match("/pay") then

end
1 Like

The script already confirms that /pay is part of the script. What I think the script should be is:

if Part[1] == '/pay' then
 local Target == Part[2]
 game.ReplicatedStorage.PlayerList --(From here it should look for Part [2] using the letters provided ad should find the player even if only one letter is given, like some sort of Auto-Find for instance names...)
end

My bad I meant the other way around try this

if "/pay":match(Part[1]) then

end

This will check if the message is in “/pay” and not equal too

Also it should be

local part2 = Part[2] —You put ==

And the player list in ReplicatedStorage is unnecessary you can just do this:

if game:GetService("Player"):FindFirstChild(part2) then
     — do stuff to player
end
1 Like

Sorry, I dont think you’re understanding me;

Basically, the server recognizes the /pay and recognizes that the player said /pay.
What I need is for it to automatically find the instance located in game.ReplicatedStorage.PlayerList .

An example is:
If I type /pay s
it should automatically look for the first instance starting with an s in game.ReplicatedStorage.PlayerList. This is to make it so that the player doesn’t have to type the full name of the target they are paying.

Oh ok so well there first thing that comes to mind is looping through the player list and checking if the name has an s or what the player said something like:

local playerFound = false
local playerList = game.ReplicatedStorage.PlayerList
for _, player in pairs(playerList:GetChildren()) do
    if not playerFound then
        if player.Name:match(Part[2]) then
            — Do stuff to that player
            playerFound = true
        end
    end
end

The player found variable is so it only gets the first one! This was written on mobile and untested so there could be errors!

It works well but is there anyway you can make it so that it finds the first one in alphabetical order?

I added a new instance that I wanted to use in order to test if it works with instances with the same starting letter but different letters onwards.

image_2021-07-31_211722

I also made it so that it prints the Instance’s name when it finds it.
image

the results were that it ended up printing the sillytothe789 instead of sa which should be the one that’s printed rather than sillytothe789 which has a second letter that is lower down the alphabet than sa’s. If there’s any way you can help, i’d really appreciate it.

Try this:

local playersFound = {}
local playerList = game.ReplicatedStorage.PlayerList
for _, player in pairs(playerList:GetChildren()) do
    if not playerFound then
        if player.Name:match(Part[2]) then
            — Do stuff to that player
            table.insert(playersFound, player.Name)
        end
    end
end

table.sort( playersFound, function ( a, b )
    -- you should return true when a comes before b
    return a.Name:lower() < b.Name:lower()
end )

print(playersFound[1]) — first index should hopefully be the correct one in alphabetical order

So this should hopefully store all the players found with that letter in a table array, then sort through that table array and put it in alphabetical order. Hopefully it will worked once again untested and on mobile, also never used table.sort but found it here.

1 Like