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

How to string.find a specific player?

Asked by
Scaii_0 145
5 years ago

I know you can only do this with strings but is there a way to find a player from someone typing their name partially?

Is there a way to format a list of all the players as a string and then find the one you need

For example:

string =  {player1,player2}
string.find(string,1)
print(the value with '1' in it)
1
use string.match() User#23365 30 — 5y
0
what do you mean by typing in, the user typing in chat or in a textbox? User#23365 30 — 5y
0
I mean in a text box which then changes a value to the text they typed. Scaii_0 145 — 5y

2 answers

Log in to vote
2
Answered by 5 years ago

finding a way to find a player by partial is relatively easy:

function findPlayer(msg)
    local msg = msg:lower()
    for _, v in pairs(game.Players:GetPlayers()) do
        local name = v.Name:lower():sub(0, msg:len())
        if name == msg then
            return v
        end
    end
    return nil
end

This works in the example:

function findPlayer(msg)
    local msg = msg:lower()
    for _, v in pairs(game.Players:GetPlayers()) do
        local name = v.Name:lower():sub(0, msg:len())
        if name == msg then
            return v
        end
    end
    return nil
end

game.Players.PlayerAdded:Connect(function(player)
    player.Chatted:Connect(function(msg)
        print(findPlayer(msg).Name)
    end)
end)
1
if you wish to make this compatible with a table change "game.Players:GetPlayers())" to the table name and change v.Name to just v Potatofoox 129 — 5y
Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

you can use string.match() to find a specific character or word inside a string. Such as this example:

--client
local TextBox = script.Parent -- lets say the script was located inside the textbox

TextBox:GetPropertyChangedSignal("Text"):Connect(function()
    for _,player in pairs(game.Players:GetPlayers()) do 
        local lower = string.lower(TextBox.Text)
        local lowerplayer = string.lower(player.Name)

        if string.match(lowerplayer,lower) then -- if the player name has a word or letter inside the string then
            print(player.Name) -- prints the player's name
        end
    end
end)

string.lower() turns the string specified letters to lowercase, since string.match() also includes capital and lowercase letters too. :GetPropertyChangedSignal() fires when the property inside the parentheses changes.

string manipulation

Answer this question