Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to remove part of a string and only show the number?

Asked by 5 years ago
Edited 5 years ago

The title may sound a bit confusing but I'll make an example.

I'm using DataStore to save some player equipment, but to retreive some values they need to come in a string and numbers to avoid needing to create an addictional DataStore.

Let's say in the following script I get a string value with the value "Pills_5". "Pills" is the name of the item and the number is the number of the items the player has. I need to remove the "Pills_" and only leave the number so I can set that number as the anmount of "Pills" the player has. Here's a short version of a script of what I want to do:

--DataS is a table with the values which was retreived from the DataStore
print(dataS.Pills) --Result: Pills_5, I need to remove the "Pills_"

player.Items.Pills.Name = "Pills"
player.Items.Pills.Value = dataS.Pills --Need to only show "5" and remove the "Pills_"


How would I do so? Is it possible to save a table such as:

local MyBoitifullTable = {Pills = 5,Medkit = 2}

and then retreive it?

Help is appreciated!

0
use string.match, and idk if you could also do that with a dictionary User#23365 30 — 5y
1
use string.sub("Pills_5", 6). #"Pills_5" is 7, meaning Pills_ is 6, so anything after the sixth substring will be returned. 5 will be the output User#19524 175 — 5y
2
or you can also do string.match("Pills_5", "%d") User#19524 175 — 5y
0
Mind making that an answer so it's a bit easier to understand and so I can give you some rep? wilsonsilva007 373 — 5y
View all comments (3 more)
1
you can upvote comments User#19524 175 — 5y
1
To make it apply for any situation just do local num = string.match(dataFromDataStore, "%d") and you will be able to access the amount of items that they have. User#21908 42 — 5y
0
Alright, thanks guys. wilsonsilva007 373 — 5y

Answer this question