I am making an admin system and it successfully gets the player but I would like to set it so the admin doesn’t have to type out the full player username in order to run a command on a specific player.
Nothing I’ve tried has worked yet and I have searched on the forums for anything related but nothing comes up, is there a way to do this?
local Players = game:GetService("Players")
local function get_player(name: string): Player | nil
name = name:lower()
for _, player in ipairs(Players:GetPlayers()) do
if name == player.Name:lower():sub(1, #name) then
return player
end
end
return nil
end
So what it does first is, lowercase the input to remove case sensitivty, then goes through all the players and removes case sensitivity by lowercasing the names of the players as well, and then only gets the portion of the string that is the 1st character and however long the input string is.
So for example you can expect
print(get_player("doe_john"))
to return @Doe_JohnRBLX reliably assuming they are in the server
There is a way utilizing gmatch and the string pattern [^,]+ (which I don’t 100% understand it). For example,
local Targets = "plr1,plr2,admins,team-raiders"
for Word in Targets:gmatch("[^,]+") do
print(Word)
end
--[[
Output:
plr1
plr2
admins
team-raiders
--]]
Hope this helped.
EDIT
Decided to write out the code for proper format.
if Targets == "all" then
return game.Players:GetPlayers()
elseif Targets == "others" then
-- Code that returns other players
else
for Word in Targets:gmatch("[^,]+") do
if Word == "me" then
return {Speaker}
elseif Word == "admins" then
-- Code that gets admins
else
for _, Target in ipairs(game.Players:GetPlayers()) do
if Target.Name:sub(1, #Word):lower() == Word then
return {Target}
end
end
end
end
end