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

How to filter through players with part of their name?

So, I'm working on my admin commands and I'm wondering how I would go about searching through the players if I have just part of their name in a string. For example, if the string was "ender" and the player "Endergenius100" was in the game, then I would run the command on that player. So far, I have tried string.match("ender","^+"..v.Name), but that didn't work. I'm very new to string manipulation, and help would be appreciated. Thanks!

1 comment

0

i got the same problem

1 answer

✓ Accepted answer

You could accomplish this by matching the the letters in this case "ender" with all the players in the player list.

For example if you wanted to have an admin command to kill someone you'd do ":kill ender"

--// Looping through all the players in the game
for _, Player in pairs(Players:GetChildren()) do
	--// Making the players name lowercase
	local makeLower = string.lower(Player.Name)
	
	--// Use :find() to find any player name that matches whatever you typed
	--// INPUT is whatever you used to break up the string message to get
	--// the players name 
	if makeLower:find(INPUT) then
		--// Just as an example you can get their character ect.
		local Char = Player.Character
		local Hum = Char.Humanoid 
		
		Hum.Health = 0
	end
end

So this would kill every player that matches so if you did ":kill s" it would kill every player who has a name starting with the letter s.

In some cases this is nice but if you don't want that to happen you could:

for _, Player in pairs(Players:GetChildren()) do
	local makeLower = string.lower(Player.Name)
	if makeLower:find(INPUT) then
		local Char = Player.Character
		local Hum = Char.Humanoid 
		Hum.Health = 0
		--// Add a break here to stop the code from looking for more matches
		break
	end
end

2 comments

0

Thanks so much! I had no idea there was a ":find" function.

0

No problem, glad it helped.

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