Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to make admin commands find player from incomplete username?

Asked by 4 years ago

So I have an admin command script, it works fine, but its a pain typing out the entire username of a player to do a command, I want the effect admin scripts such as Kohl's have, where a player can be found just from the start of their username

local function findplayer(name)
    for _,player in pairs(game.Players:GetPlayers()) do
        if string.lower(player.Name) == name then
            return player
        end
    end
    return nil
end

This is the current function that finds the player, anyone know how I can edit it to make this possible?

2 answers

Log in to vote
1
Answered by
Leamir 3138 Moderation Voter Community Moderator
4 years ago
local function findplayer(name)
    for _,player in pairs(game.Players:GetPlayers()) do
        if string.find ( string.lower(player.Name), name) then
            return player
        end
    end
    return nil
end

0
Thanks! this did the job NxvaRBLX 16 — 4y
Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

You would use string.match().

local function findplayer(name)
    for _,player in pairs(game.Players:GetPlayers()) do
    local plr = string.lower(player.Name)
        if string.match(name, plr) then
            return player
        end
    end
    return nil
end

0
I wasn't aware of that, thanks. Still didn't fix it unfortunately NxvaRBLX 16 — 4y

Answer this question