I am currently working on creating some custom admin commands.
I would like to make a function that gets the partial name, and find find a name that starts with the partial name. I would just like to make a admin command system similar to Basic Admin Essentials 2.0 - Roblox
I was thinking for using i, v in pairs() loop, however, I am not sure how this can be done.
Here is what I have so far:
local function getFullUsername(partialUsername)
for i, v in pairs(game.Players:GetChildren()) do
???
end
end)
local GetPlayer = function(string)
local lower = string:lower()
for _, plr in next, game.Players:GetPlayers() do
if plr.Name:sub(1,#string):lower() == lower then
return plr
end
end
end
For example if the player’s name is ROBLOX, in this script, will it only work if the input of the function was R, RO, ROB, ROBL, ROBLO, or ROBLOX? Or, would this work if the input was BLOX? I only want a to allow the function to get a player’s name from the first letters of a username just like Basic Admin Essentials 2.0 - Roblox
Would this script be able to solve this issue?
Alright. This is exactly what I wanted. I will test it out soon since I am currently unable to do so.
I will mark this as the solution after testing it. Thank you for your help!
local function getPlayer(str)
for _, plr in ipairs(game:GetService("Players"):GetPlayers()) do
if plr.Name:lower():find("^"..str:lower()) then
return plr
end
end
end
I don’t know if this is more or less efficient than the method @Feedekaiser provided, but there you go.
Slight problem with this design, tho
say theres 2 players “Exploiter” and “IHateExploit”
when you put “Exploit”, it might return you “IHateExploit” instead as it uses string.find. This is a big problem if you were to ban players (or something of the likes)
I’m pretty sure putting “^” at the beginning of the string.find method text parameter causes it to check if the string begins with the provided text, instead of checking whether it contains it.
local function get_player(player_name)
local lower_player_name = string.lower(player_name)
for _, player in pairs(game.Players:GetPlayers()) do
if player.Name:sub(1,#player_name):lower() == lower_player_name then
return player
end
end
end