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

How do I search for players in game.Players with only typing in a part of there name?

Asked by 6 years ago

I'm currently making an admin but I'm not so good with strings, I am unsure on how to make a command that goes like this \/

:kill xii

"xiiHearth has been killed."

This is what I have so far

function findPlayer(name)
    for _, player in pairs(game.Players:GetPlayers()) do
        if player.Name:lower() == name:lower() then
            return player
        end
    end
end
0
Use a loop to start with, for i,v in pairs(game.Players;GetPlayers() end) and than add string things to it, for i,plr in pairs(game.Players:GetPlayers()) do if plr.Name:lower():sub(1,PlrName:len()) == PlrName:lower() then -- FOUND PLAYER end end) User#20388 0 — 6y

4 answers

Log in to vote
1
Answered by 6 years ago
function findPlayer(name)
    local data = ""
    for _, player in pairs(game.Players:GetPlayers()) do
        data = ""
        for i = 1, string.len(name) do
            data = data..string.sub(player.Name, i, i)
        end
        if string.lower(data) == string.lower(name) then
            return player
        end
    end
end

I think this is what you actually want, this would return the player with the a part of it's name, it works same as you want your function to work, I am not explaining stuffs because other answers already explained them. I hope it would help, thanks for reading.

Ad
Log in to vote
0
Answered by 6 years ago

I believe you are looking for string patterns.

You will likely want to use :match()

I may not be right but I believe the code for something like this would look something like this.

local playerName = player.Name:lower()

if playerName:match("^%a+") == target.Name:lower() then
    print("target Found")
end

This basically lowers the players name, the "^" anchors the match to start at the start of the string, the '%a' implies all characters that match and the "+" means one or more characters of. Then it compares to the target name. (I may be wrong just tell me if I am)

Log in to vote
0
Answered by
fredfishy 833 Moderation Voter
6 years ago
Edited 6 years ago

The string.sub(s, i, [j]) function returns a substring of s, starting at position i until position j. If j is omitted, the substring is from position i to the end of s.

For example

string.sub("Hello", 2, 4) would yield "ell"

string.sub("Hello", 2) would yield "ello"

function findPlayer(name)
    matches = {}
    for _, player in pairs(game:GetService("Players"):GetPlayers()) do
        n = player.Name:lower()
        nFragment = string.sub(n, 1, string.len(name))
        if nFragment == name then
            table.insert(matches, player)
        end
    end

    if #matches  == 0 then
        -- no matches
    elseif #matches == 1 then
        return matches[1]
    else
        -- ambiguous
    end
end

Edit

Much better solution using match, thanks to SinisterMemories

function findPlayer(name)
    matches = {}
    for _, player in pairs(game:GetService("Players"):GetPlayers()) do
        if (string.match(player.Name, ".*" .. name .. ".*")) then
            table.insert(matches, player)
        end
    end

    if #matches  == 0 then
        -- no matches
    elseif #matches == 1 then
        return matches[1]
    else
        -- ambiguous
    end
end
0
This worked for me, thanks alot! xiiHearth 15 — 6y
Log in to vote
0
Answered by
Sir_Melio 221 Moderation Voter
6 years ago
Edited 6 years ago

You're likely wanting to fetch the first player whose username starts with the search you've input.

local players = game:FindService("Players")

local function findPlayers(search)
    search = search:lower()
    local found = {}
    for _, player in pairs(players:GetPlayers()) do
        if player.Name:sub(1, #search):lower() == search then
            found[#found + 1] = player
        end
    end
    return found
end

local players = findPlayers("Yan") -- findPlayers will always return a table, empty or not
print(string.format("Found %d player%s", #players, #players == 1 and "" or "s")) -- No need to worry about string.format, you don't need to use that.

-- Loop through them
for _, player in pairs(players) do
    print(player.Name)
end

You don't need to use any other string functions.

Answer this question