Question regarding findplayer algorithm

Okay so, im working on a adminsystem called SkyOS. In order to ban, teleport or kick someone i need to get them a instance and i have done that ba using a “findplayer” function. Now to my question: it works perfectly fine, but i always have to type in the entire name of a person and would like it to be so it finds me as a instance if i just type in “;kick flau” for example. Does anybody know how to do this using my function?

The Code
local function findplayer(name)
    for i, player in pairs(game.Players:GetPlayers())do
        if (type(name) == "string") then
            if string.match(player.Name:lower(), name:lower()) then
                return player 
            end
        else
            if (player.Name == name.Name) then
                return player
            end
        end
    end
     return nil
end

Very simple generic for loop to check for any player whose name matches [x] amount of letters to your string.

local Players = game:GetService("Players")
local function FindPlayer(name)
    for _,player in pairs(Players:GetPlayers()) do
        if string.lower(string.sub(player.Name,1,#name)) == string.lower(name) then
            return player
        end
    end
end

Alternatively if you want every player whose name’s first [x] amount of letters matches the sub string you’ve supplied, you can cache the player in a table and return the table after the for loop runs a full cycle.

local Players = game:GetService("Players")
local function FindPlayer(name)
    local playersFound = {}
    for _,player in pairs(Players:GetPlayers()) do
        if string.lower(string.sub(player.Name,1,#name)) == string.lower(name) then
            table.insert(playersFound,1,player)
        end
    end
    return playersFound
end
1 Like
local players = game:GetService("Players")

local function findplayer(name)
    -- convert name to lower case and remove spaces and other
    -- extraneous characters not allowed in usernames
    name = string.gsub(string.lower(name), "[^a-z0-9_]", "")

    for _, player in next, players:GetPlayers() do
        local playername = string.lower(player.name)

        if string.sub(playername, 0, #name) == name then
            return player
        end
    end
end
1 Like

So uh. I tried your first code. And it just throws this error at me: Screenshot by Lightshot

And this is the error im getting with your function: Screenshot by Lightshot

That’s because it’s expecting a string (Player name), not a Player Instance. You’d have to modify as you did in your original script to accept a Player Instance.

1 Like

thats weird, since what im feeding in is a string.

so, thats the code where the function is being called in: Screenshot by Lightshot

local plrtotp = findplayer(playertotp.Name)
1 Like

oh wit nvm, it gets the error when probably going trough the sender

1 Like

yep, i guessed that now after taking a close look

1 Like

If the sender is the player to teleport then you don’t need to use findplayer, as you can guarantee they exist and you already know who they are

p.s. don’t forget to mark a solution if any of us have helped you

2 Likes
local function findplayer(name)
    for i, player in pairs(game.Players:GetPlayers())do
        if (type(name) == "string") then
            if string.match(player.Name:sub(1,#name):lower(), name:lower()) and #name <= #player.Name then
                return player 
            end
        else
            if (player.Name == name.Name) then
                return player
            end
        end
    end
     return nil
end

allright! Thanks for your help mate. It now finally worked and i can release the alpha of my adminsystem!

And also thanks to you and @Imp_erator for trying to help!