Shortening Players name { admin commands }

Started messing around with Admin Commands. So far, works fine with no issues. However, how could I adjust my coding below to allow for locating a Player by a shortened name rather than having to type the entirety of their usename(s) ?

function Register(Player, Msg)
	if Msg:sub(1, 1) == Prefix then
		for _,i in pairs (Cmds) do
			if (string.find(Msg:lower(), i:lower()) ~= nil) then
				Cmd = i
				break
			else
				Cmd = nil
			end
		end
		for _,i in pairs (Players:GetPlayers()) do
			if (string.find(Msg:lower(), i.Name:lower()) ~= nil) then
				Plyr = i
				break
			else
				Plyr = nil
			end
		end
	end
	CmdControl(Player)
end
2 Likes

I did not understand what you are trying to tell. Could you elaborate it? To just shorten a player’s name, you can use:

string.sub(player.Name, 1, maxLetters)

Wouldn’t that help? Also, I’m new to string, so if there are any errors please correct me.

Hi - Yes sorry. The way the code currently functions is it can locate a Players name in the Msg string - So example it’ll detect: “KossmoZ” - However, I’m looking to adjust this so it can detect suches as “K”, “Ko”, and so forth.

I’m not sure if it’s practical, but why don’t you store each letter of every player in a table and then index that when you are typing the player’s name?
Again, I would like to say, I’m new to these stuff so I would love a correction.

Recently, I made a system just like this, mainly what you need to do is compare the lowered inputted string with the Player’s name lowered and subbed to the length of the inputted string.

Here’s an example of a function to do this:

local Players = game:GetService("Players");

local function GetPlayer(Input)
    for _, Player in ipairs(Players:GetPlayers()) do
        if (string.lower(Input) == string.sub(string.lower(Player.Name), 1, #Input)) then
            return Player;
        end
    end
end

print(GetPlayer("return")) --// ReturnedTrue
2 Likes

You could use the string.sub method.
(I started typing an hour ago but got distracted)
Basically, in short,

local msg = stringtodetectplayerhere

local function findPlayer(stringg)
    for _, v in pairs(Players:GetPlayers()) do
         if stringg:lower() == (v.Name:lower()):sub(1, #stringg) then
               return v
         end
    end
end

local player = findPlayer(msg)
6 Likes

Hi I’m new to Strings and I’m getting red lines under certain sections of this code?

Sorry, fixed the typo. Look at it once more

(forgot the double equal signs and the parantheses)

1 Like