Using string.sub to set names without affecting certain part of string

So currently I have a plot system like this:
100_Basic
102_Basic
104_Basic
etc

Number being the ‘lot’ number and the Basic being the type of lot it is.

However, I’m trying to set the plot name to be like
100_Player1
102_Player2
etc

So basically setting the lot to be the players name, but keeping the number in front

local playersPlot = string.sub(plots:WaitForChild(user.Plot), 4)
playersPlot.Name = player.Name

So basically, user.Plot == ‘Basic’, so this should find an ‘empty’ plot (as empty plots are just their number with their type after)

But then setting the plots name, how can I do that with keeping the number still there?

EDIT just found a problem, doing string.sub(plots:WaitForChild(user.Plot), 4) returns ‘Basic’ (duh :man_facepalming: I should have known that)

How can I get it to find the first Basic plot in the list?

Plot models look like this:

And so basically the first player to join should get 100, and the name of that plot should be 100_Player1. Then the next player would get 102_Basic, with the name becomming 102_Player2, and so on. (hope this explains it a lil more :sweat_smile:)

If the numbers at the beginning are only going to be 3 digits(100-999) then to get the number as a string you can just sub the first 3 characters like so:

local originalName = "100_Basic"
local playerName = "cheeseMan"
print(string.sub(originalName,1,3).."_"..playerName)

outputs → 100_cheeseMan
however if you plan to use any different kind of number I would use string.match() and get digit characters like so

local originalName = "100191_Basic"
local playerName = "cheeseMan"
print(string.match(originalName,"%d+").."_"..playerName)

Outputs → 100191_cheeseMan
If you’re wondering how this works it matches the string for the pattern of '“%d+” the %d meaning digit characters and the + meaning get all digit characters in the first match we can.

Here’s a link about string patterns in lua.

How would I go about checking for their plot from other scripts? Normally I’d have this

local playersPlot = plots:WaitForChild(player.Name)

But now since I have numbers in front I can’t. Is there a way to check for the players name in a model? Preferably not from using a for loop or whatever. I’d like to be able to just get it from one line

In that case, I would either save a value in a value object or a table, which tells the player and the player’s plot name or just loop through all the plots and see if the name of the plot of has the player’s name in it using string.match. The best one-line option would just be to save the plot in an object value or string value or in a dictionary, where the key is the player’s name and the value is the plot or plot name. (If it should be accessible to other scripts use a module script)