String.Match Help

I want to make a text box auto complete usernames so if I say “drag” it will reference “DragonPlayzz”
I’ve looked on YouTube but I can’t find any high quality tutorials for it. I have found some here on the forum but I’m a beginner scripted and it’s too complicated for me.

Any steps in the right direction would help greatly! Thanks.

A portion of a string is known as a substring. You can use string.sub to extract a substring out of a string.

local Players = game:GetService("Players")

local function get_player_from_partial(str)
    str = str:lower() -- Removes case sensitivity

    for _, player in ipairs(Players:GetPlayers()) do
        if player.Name:lower():sub(1, #str) == str then
            return player
        end
    end
    return nil
end

So what has been done?

The function goes through all the players in the game. It gets the name of each player, lowercases it to remove case sensitivity, extracts the name of each player, starting at the first character to however long the provided string is. It will then return that player.

Otherwise it returns nil.

local player = get_player_from_partial("inc") -- say I am in the game
print(player) -- incapaz
4 Likes