My idea for your situation is this:
You want to search through every player in the game and see if anyone's name starts with the input that the user provides.
I'm assuming here that you are only accepting inputs that are the beginnings of a player name, for example ;stats blu is accepted but ;stats streake is not. If you'd like to accept the latter, you will have to check if the input string matches ANY part of a player name. This isn't much different but it's up to you which you prefer.
To do this, an important tool you will need is string.sub which has the following parameters:
string s - the string you wish to manipulate
int i - the position you want your substring to begin at
int j [optional] - the position you want your substring to end at (inclusive)
Now, you need a way to search the input through every player. This can be done using Players:GetPlayers(). Personally, I would have a function that would return a table of all users that match the input. My function would look something like this:
local function getRequestedPlayers(requestedName)
local returnPlayers = {}
for _, player in pairs(Players:GetPlayers()) do
if string.sub(player.Name, 1, #requestedName) == requestedName then
table.insert(returnPlayers, player)
return player
end
end
return returnPlayers
end
As shown above, it's your decision if you want all the players that match the input or just the first one.
Now, you can replace line 8 in your code with getRequestedPlayers(player) and work with that.
Hope this helps.
EDIT:
If you choose to go with the method that accepts every player that matches the input (for example, ;stats ad would request admin1, admin2, admin3, then you can simply use a for loop immediately after requesting players like so:
if #foundPlayers > 0 then
for _,foundPlayer in pairs(foundPlayers) do
end
end
4 comments
I'm not sure I understand what you mean. You want a command that will set a player's name to a nickname you define? The code you posted seems to be a command that sets a player's leaderstats.
It is, but instead of having to type out a player's whole name, I'd only have to type two or three characters of it. I know this involves string manipulation, but I don't know which method that I should use, or how to use that method.
string.sub and string.len
I figured I'd have to use string.len. How would I use it though?