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:
1 | string = { player 1 ,player 2 } |
2 | string.find(string, 1 ) |
3 | print (the value with '1' in it) |
finding a way to find a player by partial is relatively easy:
01 | function 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 |
10 | end |
This works in the example:
01 | function 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 |
10 | end |
11 |
12 | game.Players.PlayerAdded:Connect( function (player) |
13 | player.Chatted:Connect( function (msg) |
14 | print (findPlayer(msg).Name) |
15 | end ) |
16 | end ) |
you can use string.match()
to find a specific character or word inside a string. Such as this example:
01 | --client |
02 | local TextBox = script.Parent -- lets say the script was located inside the textbox |
03 |
04 | TextBox: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 |
13 | 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.