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

How do I located users in a text box??

Edd= script.Parent.Parent
function getPlayer(name)
 local returned = {}
 for i, v in pairs(game.Players:GetPlayers()) do
  if v.Name:sub(1, Edd.BoxPlayer.Text()):lower() == name:lower() then
   table.insert(returned, v)
  end
 end
 return returned
end?

I am trying to do a admin panel and when someone types like a short version of a name to do that command on a text box. For example The text box would say y8 then you would kill y8pogo123589 when you press the kill button.

1 answer

Match function


There main way you would do this is with the string.match function, and iterating thought the list of players:

The way the match function is formatted is like such:

string.match(String,Pattern,start,finish)

Note that these aren't the official names used on the wiki.

The Match function returns either pattern that is found, or nil, which makes it a great way to detect is a string contains a particular pattern.


Application


With that said, here would be an example:

local function GetPlayer(Text)
	local plrs = {}
	for _,plr in pairs(game.Players:GetPlayers()) do
		if (plr.Name:lower()):match(Text) then
		 	table.insert(plr,plrs)
		end
	end
	return plrs
end

One other thing to note is that it is better practice to use local variables in the global scope rather than global variables (variables defined without local).

Hopefully This Helped!

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