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 6 years ago
Edited 6 years ago

For example

playerstats = "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
6 years ago
Edited 6 years ago
local PrepareS = function(String)
    local String = tostring(String)  -- Allows you to run the code with numbers.
    local Pattern = "%d%d"
    local XP = String:sub(3, string.len(String))
    local Lvl = String:match(Pattern)
    return Lvl, XP
end

print(PrepareS("2358394"))  -- Just examples of it working.
print(PrepareS(23594))
print(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