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
6 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:

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

2 answers

Log in to vote
2
Answered by 6 years ago

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

01function findPlayer(msg)
02    local msg = msg:lower()
03    for _, v in pairs(game.Players:GetPlayers()) do
04        local name = v.Name:lower():sub(0, msg:len())
05        if name == msg then
06            return v
07        end
08    end
09    return nil
10end

This works in the example:

01function findPlayer(msg)
02    local msg = msg:lower()
03    for _, v in pairs(game.Players:GetPlayers()) do
04        local name = v.Name:lower():sub(0, msg:len())
05        if name == msg then
06            return v
07        end
08    end
09    return nil
10end
11 
12game.Players.PlayerAdded:Connect(function(player)
13    player.Chatted:Connect(function(msg)
14        print(findPlayer(msg).Name)
15    end)
16end)
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 — 6y
Ad
Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

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

01--client
02local TextBox = script.Parent -- lets say the script was located inside the textbox
03 
04TextBox:GetPropertyChangedSignal("Text"):Connect(function()
05    for _,player in pairs(game.Players:GetPlayers()) do
06        local lower = string.lower(TextBox.Text)
07        local lowerplayer = string.lower(player.Name)
08 
09        if string.match(lowerplayer,lower) then -- if the player name has a word or letter inside the string then
10            print(player.Name) -- prints the player's name
11        end
12    end
13end)

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