Checking if the name has a specific letter

I am working with combinations in my game and I have hit a problem. So far, the player creates a random combination by adding whatever ingredients they want. Every time they add a new ingredient, it adds a specific letter to a part in the model. My goal is for the server to read the name of the part in the model so it knows which model to take out of the replicated storage that exactly matches the player’s combination.

For example, assume we have 3 ingredients each with an assigned letter:
Apple: A
Banana: B
Carrot: C
If the player adds all 3 ingredients, the name the server will read will say ABC. If the player only chooses Apple and Banana the name will be AB.

My issue is that the player can add the ingredients in random orders to the name can be BCA or BA and it won’t match what the server wants to see. Now I could just tell the server to read every different combination those letters can make but that would take so long for the server to find the correct combination.

My question is, is there a way to script it so that it can check if the name HAS a specific letter. So it checks if the name has an A, or b, or c, or a combination of ABC in any order?

Thanks for those who read this far.

You can use str:match("[ABC]"). [ABC] is not a sequence, it will match each letter individually.

-- There is at least a C so it will be returned
> print(string.match("Cousin", "[ABC]"))
C
1 Like

You can use string.find to find whether the string has a specific letter.
For example,

local test_string = "BACD"

print(string.find(test_string, "C")) -- prints 3
print(string.find(test_string, "E")) -- prints nil

Here is the reference from the docs:

Looks for the first match of pattern in the string s. If it finds a match, then find returns the indices of s where this occurrence starts and ends; otherwise, it returns nil. A third, optional numerical argument init specifies where to start the search; its default value is 1 and can be negative. A value of true as a fourth, optional argument plain turns off the pattern matching facilities, so the function does a plain “find substring” operation, with no characters in the pattern being considered “magic”. Note that if plain is given, then init must be given as well.

(Source: string | Documentation - Roblox Creator Hub)

2 Likes

I’m new to LUA so I’m not sure how your solution works and how tables really works. Can you explain what Cousin is and what it will print out.

I see how string.find works, but how would I make it so that if both of the letters are there AND ONLY those letters than it would run a command. Since ACE and CE both have those two letters so won’t it print for both those combinations?

Lua*
Name, not acronym :slight_smile:

My solution is not using tables at all. If you printed my example in the output:

Because [ABC] matched the letter C.

1 Like

You can use string.find inside for loop to check for each letter of the specific string like this:

local TableOfCharacters = {
	"A",
	"B",
	"C"
}

local String = "ABC"

for i=1,#String do -- or string.len(String) instead of #String
	print(string.find(String,TableOfCharacters[i]))
end

--Output:
--  1 1
--  2 2
--  3 3
1 Like

My bad lol. I get what cousin and ABC means now.

1 Like