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

Can you read specific characters from a string?

Asked by 7 years ago
Edited 7 years ago

For example

1playerstats = "5565320"

(Where LVL = "55" & XP = "65,320")

Could you do something to find what characters #3 thru #7 read, returning "65320"?

If this is possible please answer with the function.

I'm trying to use this to make a players datastore one singular string but reading out dozens of information, therefore not even coming close to the datastore limit and making datastores more efficient (for me anyways).

1 answer

Log in to vote
1
Answered by
Tomstah 401 Moderation Voter
7 years ago
Edited 7 years ago
01local PrepareS = function(String)
02    local String = tostring(String)  -- Allows you to run the code with numbers.
03    local Pattern = "%d%d"
04    local XP = String:sub(3, string.len(String))
05    local Lvl = String:match(Pattern)
06    return Lvl, XP
07end
08 
09print(PrepareS("2358394"))  -- Just examples of it working.
10print(PrepareS(23594))
11print(PrepareS(12458))

There ya go.

Pattern is for the first two digits, it searches the given string for the first two digits. The sub() function then shortens another version of the string to the 3rd digit and the end of the string and then returns that. Should be what you need. (Also, input is a string so if you input numbers it won't work so just run to string first.)

Ad

Answer this question