How to remove the first few letters off a string?

I am terrible with string manipulation, and just can’t seem to figure this out without getting the error of ‘unable to cast string to int64’. I have currently got the string of ‘Player_175157011’ (Coming from a datastore) and want to cut down to just the player Id, ‘175157011’ to get their username. However, how would I just cut them first few letters off? Would I use string.sub or something else?

You can use string.match to only get the number portion of the string. The pattern “%d+” represents one or more numbers, which can be used along with match to return the number portion of the string. Additionally, if you need both pieces of the string, you can use string.split and split it in half at “_”.

I also want to remove the underscore, would that also get rid of it?

string.match only returns the pattern if it is matched. So in your case that would only return the numbers (175157011).

Would it still work even with different IDs? That was just an example Id, but there will obviously be more than one Id that needs to be changed.

Consider the following:

local samples = {
  "Player_12389543",
  "Player_23489224",
  "Player_32849829"
}

local pFormat = "%s contains the id: %s"

for k,v in next, samples do
  print(pFormat:format(v, v:match("%d+")))
end

Yes, it will work with any id since the pattern will match one or more numbers.

I think string.split was an easier solution, the code that I used was:

local NameSplit = name:split("_")
print(NameSplit[2])

As it gives me a table of values, I had to print the second value of the table which was the Id. I think this was quite similar to what string.match does.

I would use string.match since the “Player” portion of the string is unused. However, either solution is fine as long as it works for your needs.

EDIT: It would look like this

local userId = name:match("%d+")
print(userId)

I just ended up setting the value to nil after because the Id won’t be needed again.

I guess you could use this, it isn’t too efficient but it works.

local UserId = tonumber(string.sub("Player_175157011", 8))