How can I get a player without their full username?

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?

2 Likes

can you show it with image?
Because i need see it

You can make your own function for 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

7 Likes

Thank you, I will try implementing it now!

2 Likes

I will reply with the code if @sjr04’s solution doesn’t work.

2 Likes

ok,
I’ll come if I need help. :wink:

2 Likes

Would there also be a way to implement parameters into the get_player function such as ‘me’ ‘all’ ‘others’ ‘admins’ ‘nonadmins’ etc?

Of course applicable, but a script like this HD Admin may be required.

1 Like

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. :slight_smile:

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

@WinkleSociety

1 Like

Hm, I will try and find a way to implement it, thanks :+1:

1 Like

Hello again, how would I go about implementing your solution into this code here:

image

Simply replace the FindFirstChild bit with the GetPlayer function. ;p

local player = Players:FindFirstChild(args[1])
-- to
local player = get_player(args[1])

Thanks so much, I have been so tired lately and I haven’t been able to script efficiently so thanks for the help :grin:

2 Likes