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?
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.
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?
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