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

Find a player with only a part of their username?

Asked by
awfulszn 394 Moderation Voter
5 years ago

I am attempting to find a player when only typing in a part of their username in chat, however what I have managed to do is print all the players names if their name had a 'h' in it, if I typed the letter 'h', no matter where in the name it is. I am trying to make it so it reads from the start of the name, not just look for if the name has that specific string anywhere in it.

plr.Chatted:Connect(function(msg)
    for _, v in pairs(game.Players:GetPlayers()) do
        if v.Name:lower():find(msg:lower(), 1, true) then
            print(v.Name)
        end
    end
end)

Any help is appreciated, thanks.

1 answer

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

To accomplish this, you would the string.sub function. sub stands for substring. Here's a little explanation by Roblox about this function:

"Returns the substring of s that starts at i and continues until j; i and j may be negative. If j is absent, then it is assumed to be equal to the length of s. In particular, the call string.sub(s,1,j) returns s until as many characters as j, and string.sub(s, -i) returns a suffix of s with length i."
plr.Chatted:Connect(function(msg)
    local Msg = msg:lower()

    for _, v in pairs(game.Players:GetPlayers()) do

        if Msg == v.Name:lower():sub(1, #Msg) then
            print(v.Name)
        end
    end
end)

0
`Msg:sub(1)` That's redundant: It's the same as without using `:sub(1)`. Also, why not put line 3 before the loop? TheeDeathCaster 2368 — 5y
0
i forgot lol, and what is wrong with me User#19524 175 — 5y
0
Probably the wheather. lol TheeDeathCaster 2368 — 5y
0
Worked perfectly, thank you. awfulszn 394 — 5y
Ad

Answer this question