How do I get a player's name with only few characters?

Hi, I’m making a kick command, like /“kick DarkKnightPlayz145” but, I don’t wanna type the full name of the player. How would I do that? Help is appreciated!

3 Likes

The method to do this, is to get the list of player names on the server.
This would just be iterating through the children of the Player’s folder. Use string.sub to get the partial string from the player name and match it to the inputted name in the command.
local tokick = “PartialNameEntered”
for i,player in pairs(game.Players:GetChildren()) do
if string.sub(player.Name,1,#tokick,)== tokick then
—kick player code here
end
end

2 Likes

Thanks. Let me try it. Thanks so much

1 Like

Keep in mind. This code will require the exact captialization/correct symbols as it will check for an exact match between the two strings. Also it has to be done globally. So you have to send a remote event to do it from the local chat. Or… scratch that. I don’t know if chat input is global or local. One way or another the input itself has to be brought to the kick function which has to be server sided (for security reasons)

1 Like

Oh okay, like if the name is “CrazyMan1212” so I can type “/kick Crazy” but I can’t type “/kick crazy”

1 Like

Yes. Exactly. If you want to allow for capitalization, you would have to use some other string functions
to be able to do so.

Thanks, now I’ll go play some games.

1 Like

The function you are looking for there btw is :Lower()

string.lower(string) to be exact.

1 Like

Or string = string:Lower() either one. It’s a matter of preference at that point.

Problem with that is the inserted partial name would also match if it wasn’t in the beginning of the name, use this sub pattern instead if you don’t want it to match by accident with a different player.

For example, say you have partial name “Cat” and you have two players named CoolCat123 and Catty100, you probably want it to match Catty100 and not CoolCat123, thus you do this:

string.match(player.Name, "^"..tokick)
Full Code
local tokick = “PartialNameEntered”
for _,player in ipairs(game.Players:GetPlayers()) do
    if string.match(player.Name, "^"..tokick) then
        --kick player code here
    end
end

Also if you want to do non-case sensitive, you can do string.lower() for the partial name and player name during the matching.

How do you mean it would match other things in the player? String.sub has a start point and an end point for reference. It only matches the strings that are entered from start to n. It checks if the n number of letters from 1 to n where n = #inputtednamefromthecommand Matches the 1 to n of the player name.
String.sub takes three args. The string,the character in the string to start at, and the character in the string to end at.

1 Like

My mistake, I thought you were using match and I forgot it was sub. The results will be the same otherwise.

1 Like

All good. Just less work for the .sub because you are only checking the inputted amount of text from start to finish.

1 Like