Getting player name from partial text

Hello Roblox community!

I am currently working on creating some custom admin commands.
I would like to make a function that gets the partial name, and find find a name that starts with the partial name. I would just like to make a admin command system similar to Basic Admin Essentials 2.0 - Roblox

I was thinking for using i, v in pairs() loop, however, I am not sure how this can be done.
Here is what I have so far:

local function getFullUsername(partialUsername)
    for i, v in pairs(game.Players:GetChildren()) do
        ???
    end
end)

Does anyone have any suggestions to create this?

local GetPlayer = function(string)
  local lower = string:lower()
  for _, plr in next, game.Players:GetPlayers() do
    if plr.Name:sub(1,#string):lower() == lower then
      return plr
    end
  end
end
6 Likes

Apologies, could you give me a brief explanation of this script?

string.sub sliced the string into the length of the partial text
say
given ro

ROBLOX will be sliced into RO

string.lower returns the lowercase version of the text

for the same scenario

RO becomes ro

For example if the player’s name is ROBLOX, in this script, will it only work if the input of the function was R, RO, ROB, ROBL, ROBLO, or ROBLOX? Or, would this work if the input was BLOX? I only want a to allow the function to get a player’s name from the first letters of a username just like Basic Admin Essentials 2.0 - Roblox
Would this script be able to solve this issue?

works for every case other than BLOX

Say the name is ROBLOX, input such as

ro
rOb
Rob
robl
RoBL

works while

Ob
blox
ox
lox

doesn’t work

Alright. This is exactly what I wanted. I will test it out soon since I am currently unable to do so.
I will mark this as the solution after testing it. Thank you for your help!

This is the way I do it in all my projects:

local function getPlayer(str)
    for _, plr in ipairs(game:GetService("Players"):GetPlayers()) do
        if plr.Name:lower():find("^"..str:lower()) then
            return plr
        end
    end
end

I don’t know if this is more or less efficient than the method @Feedekaiser provided, but there you go.

Slight problem with this design, tho
say theres 2 players “Exploiter” and “IHateExploit”

when you put “Exploit”, it might return you “IHateExploit” instead as it uses string.find. This is a big problem if you were to ban players (or something of the likes)

I’m pretty sure putting “^” at the beginning of the string.find method text parameter causes it to check if the string begins with the provided text, instead of checking whether it contains it.

and what do I put on “string”?

I don’t get it, sorry

local function get_player(player_name)
    local lower_player_name = string.lower(player_name)
    for _, player in pairs(game.Players:GetPlayers()) do
        if player.Name:sub(1,#player_name):lower() == lower_player_name then
            return player
        end
    end
end

This function should be easier to understand.