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

Using string patterns to separate numbers by a character?

I'm creating an inventory system and holding items with their id and then their amount, separated by the character "x". I'm however not that good at string patterns, so I'm not sure how I would be able to increase the quantity through changing that part of the string. Example:

"14x2" -- 14 is the item's ID and 2 is the quantity

How do I use string patterns to get the 2 so I can change it to for example a 3 if another of the same item is collected? Thanks

2 comments

0

Good question, lots of information, explained well. A+.

1 answer

✓ Accepted answer

Delimiters

In this scenario, the "x" is acting as a delimiter. Delimiters separate and differentiate data within text. In this case, differentiating ID number from quantity number.

Assuming you're expecting the format of this data to remain constant (number-delimiter-number), you can simply use a string pattern that looks for the same structure. Here's an example:

local function parseItemOrder(itemOrder)
	return tonumber(itemOrder:match("%d+")), tonumber(itemOrder:match("%d+$"))
end

local itemID, itemQuant = parseItemOrder("14x2")
print(itemID, itemQuant) --> 14	2

The patterns are quite simple, but you may want to modify the code if it's possible for the format of the itemOrder to change. Hope this helps; let me know if you have any questions.

2 comments

1

Fantastic, thank you so much!

0

scriptguider you are getting close to 5000 rep! keep it up

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