Get players from a string about 3 characters (Estimate)

For example, these are the players in the server

  • James
  • David
  • Kelly
  • Shanon
  • Lissa
  • Susan
  • Bill

(Not real usernames)

.
I want to have a string like this “dav”

The script will automatically change the script to David because it is the closest player in the list. This works similarly to the "/whisper " command in the Roblox chat.

If there were players like this

  • Ben1
  • Ben2
  • Ben3

And you typed “Be”
It will return false.

How would I do this?
I tried using looking at the Wikia for the different string things and nothing came to me.

1 Like
local function get_player_from_string(str)
    str = str:lower() -- to remove case sensitivity
    local duplicate_count = -1

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

            if duplicate_count > 0 then
                return nil
            end

            return player
        end
    end
    return nil
end

Basically, if there is more than 1 occurrence of the same substring, return nil so it doesn’t send to a random player.

The rest should be self-explanatory

1 Like