String.Match() HELP

hey! i am Atlas and i got a problem /:

  1. i would like to know how to use string.match().
    lets imagine i want a script to find a part thats called hellopvppoo
    and i want the script to find it, if i type hello,pvp.poo and all other words given in there.
    how would i do that?

  2. the issue is that i dont know how to use the names of instances in combination with string.match()

i tried using string.match() but it didnt work.

any help is highly appreciated :smiley:

1 Like

string.match uses regular expressions which are pretty standard across programming languages. You can find a cheat sheet for how to make what you’re asking here: Regular Expressions Cheat Sheet by DaveChild - Download free from Cheatography - Cheatography.com: Cheat Sheets For Every Occasion

If you want to detect the presence of hello, pvp, and poo all independently with no regard for their position relative to each other, you can just use 3 separate calls to match.

Edit: here is an interactive thing that explains much more: https://regexr.com/

string.match() isn’t used like that, it’s used to tell if you if a specific keyword is inside a string. For example:

local match1 = string.match("Welcome to Roblox!", "Roblox")
local match2 = string.match("Welcome to my awesome game!", "Roblox")
print(match1) -- returns: Roblox
print(match2) -- returns: nil

The way to find if a specific instance using a name, is to use :FindFirstChild, so for example:

if workspace:FindFirstChild("hellopvppoo") then
  print("I found hellopvppoo!")
else
  print("I didn't find hellopvppoo! :(")
end

i was talking about the kind of how hd Admin knows that i mean the player called 007busterxo007 if i just type 007!
HOW does the hd admin setup do that?

1 Like

I don’t think it uses match, I have made a script that replicated that in the past, and it looks like this:


function FindPlayers(name)
	local usersTable = {}
	for i,v in pairs(players:GetPlayers()) do
		if v.Name:lower():sub(1,#name) == name:lower() then
			table.insert(usersTable,#usersTable+1,v)
		end
	end
	return usersTable
end

Function returns all players that fit the expression

But i wanted to get only the most matching instance
/:

1 Like

It knows because out of all the players only one player’s name starts with “007”, which is what that function does. You just need to check that the length of usersTable is 1.

How to do that without table?
I am trying to can do this also with parts

1 Like
local selectedPlayers = FindPlayers(playerName)
if #selectedPlayers == 1 then
	--Run command
else
	--playerName was not specific enough, don't run command
end

What is the findplayers function?

1 Like

I think , match 1 returns true, not roblox, right?
While match 2 returns false, not nil
Also right?

1 Like

No, it returns the text of the match, consistent with the historical definition of a regex matching function.

Uh. But why does it work with if statements then?

1 Like

It works because nil is considered false and all other values including a string are considered true.