GetPlayerFromAbbreviation() Custom Function

Have you ever tried creating an admin script, but always wondered how developers make it so you can abbreviate the spelling of a players name? For example, how they can do “:kick Rob” instead of “:kick Robert2311”. In this tutorial, I’ll be giving you the basic code on how to create a function that does just this, going through the steps on each line.

The function will look somewhat like this:

--> programher (Instance)

Here’s the code for the function:

function GetPlayerFromAbbreviation(abbreviation)
  for i,v in ipairs(game:GetService("Players"):GetPlayers()) do -- looping through players
    if v.Name:lower():sub(1,#abbreviation) == abbreviation:lower() then -- checking if the lowercase version of the players name (subbed) matches the lowercase version of the abbreviation
      return v -- if it does, return the players instance
      if #game:GetService("Players"):GetPlayers() == i then -- checking if the index matches the player count (AKA if there are no more players to loop through)
       return false -- returns false if no players matching the abbreviation was found
       continue -- continues the loop if there are still players to loop through

This is a basic example of how you can use this in an admin script:

local admins = {"programher"}
     local args = message:split(" ")
     if args[1]:lower() == ":kick" and admins[player.Name] then
      if args[2] then
        if GetPlayerFromAbbreviation(args[2]) ~= false then -- checking for the player
          local target = GetPlayerFromAbbreviation(args[2]) -- player exists, set the target value to the function (with args[2] as the parameter)
          target:Kick("You were kicked by an administrator.")

Remember, GetPlayerFromAbbreviation returns the players instance. Hope this helps!


what if multiple players have names that start with “pro”?

It returns the first player (according to the :GetPlayers()) array that starts with “pro”. Once returned, the whole function terminates.

Change this to ipairs so it’s done alphabetically.

I actually said “ipairs” and Mac corrected it to “pairs”.

1 Like

I don’t really get the point of this, the loop would finish running anyways and you could put the return outside of it.

I mean, it doesn’t really matter. But this specific line (technically) accounts for when a new player joins the game.

Man, Me thinking i will never see the day that people would create their own functions that seems to function just like it was a official roblox function!

Haha. I create functions like this all of the time, I decided to share a useful one with you guys. :slight_smile:

Great thing! I also wanted to add this here! Have you wondered of just calling this function again and again from different scripts? Well now you don’t!

Just go ahead and do this!

_G.GetPlayerFromAbbreviation = function(abbreviation)
    -- The code

Then calling from another script:

1 Like