Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to string.find a specific player?

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)

3 comments

1

use string.match()

0

what do you mean by typing in, the user typing in chat or in a textbox?

0

I mean in a text box which then changes a value to the text they typed.

2 answers

✓ Accepted answer

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 comment

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

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

This discussion is preserved as it was. New answers, comments, and votes are closed.