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

1function findPlayer(name)
2    for _, player in pairs(game.Players:GetPlayers()) do
3        if player.Name:lower() == name:lower() then
4            return player
5        end
6    end
7end
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
01function findPlayer(name)
02    local data = ""
03    for _, player in pairs(game.Players:GetPlayers()) do
04        data = ""
05        for i = 1, string.len(name) do
06            data = data..string.sub(player.Name, i, i)
07        end
08        if string.lower(data) == string.lower(name) then
09            return player
10        end
11    end
12end

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.

1local playerName = player.Name:lower()
2 
3if playerName:match("^%a+") == target.Name:lower() then
4    print("target Found")
5end

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"

01function findPlayer(name)
02    matches = {}
03    for _, player in pairs(game:GetService("Players"):GetPlayers()) do
04        n = player.Name:lower()
05        nFragment = string.sub(n, 1, string.len(name))
06        if nFragment == name then
07            table.insert(matches, player)
08        end
09    end
10 
11    if #matches  == 0 then
12        -- no matches
13    elseif #matches == 1 then
14        return matches[1]
15    else
16        -- ambiguous
17    end
18end

Edit

Much better solution using match, thanks to SinisterMemories

01function findPlayer(name)
02    matches = {}
03    for _, player in pairs(game:GetService("Players"):GetPlayers()) do
04        if (string.match(player.Name, ".*" .. name .. ".*")) then
05            table.insert(matches, player)
06        end
07    end
08 
09    if #matches  == 0 then
10        -- no matches
11    elseif #matches == 1 then
12        return matches[1]
13    else
14        -- ambiguous
15    end
16end
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.

01local players = game:FindService("Players")
02 
03local function findPlayers(search)
04    search = search:lower()
05    local found = {}
06    for _, player in pairs(players:GetPlayers()) do
07        if player.Name:sub(1, #search):lower() == search then
08            found[#found + 1] = player
09        end
10    end
11    return found
12end
13 
14local players = findPlayers("Yan") -- findPlayers will always return a table, empty or not
15print(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.
16 
17-- Loop through them
18for _, player in pairs(players) do
19    print(player.Name)
20end

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

Answer this question