Find player from first few letters

How could i make it so that instead of referencing a player name in, for example, a chat command i just have to say the first few letters of their user?

Example:
Player username: Oggy5000
Instead of “/bring Oggy5000” just “/bring Og”

2 Likes
function FindPlayer(String)
	for i,v in pairs(game:GetService("Players"):GetChildren()) do
		if (string.sub(string.lower(v.Name),1,string.len(String))) == string.lower(String) then
			return v
		end
	end
end
4 Likes

This would cause silent “collisions” picking the first player at random that matches, for instance if you have two players name “Oggy500” and “ogFrog” it could select either one at random.

Something to aware of, but cannot be easily fixed.

1 Like
function FindPlayers(String)
	local t = {}
	for i,v in pairs(game:GetService("Players"):GetChildren()) do
		if (string.sub(string.lower(v.Name),1,string.len(String))) == string.lower(String) then
			table.insert(t, v)
		end
	end
	return t
end

Quickly altered the snippet above to return an array of players.

1 Like