What's wrong with my code? (string:sub)

I have this script below:

local newID = tostring(script.Parent.Text)
local subbedID = newID:sub(1,-2)
subbedID = newID.."2"
subbedID = tostring(subbedID)
print(newID.." is the old id, "..subbedID)

and my goal is to set the subbedID variable to newID without the last character of the string and replace it with 2. I have been searching the devForum and other sites, and found that I should use string:sub, so I tried the above code. However it doesn’t remove the last character of the string, it just adds 2 to it. Any ideas?

Maybe this can help

You did subbedID = newID .. "2" instead of subbedID = subbedID .. "2" which made subbedID equal to newID with a 2 at the end.

local textbox = script.Parent
local newID = textbox.Text
local subbedID = newID:sub(1,-2)
subbedID = subbedID..2
print(newID.." is the old id, "..subbedID)

You don’t need to be calling tostring() as the value assigned to the “Text” property of GuiObject instances is a string value anyway. The previous post provided the correct solution, you should be concatenating “2” to the new string not the old string.

Oh my goodness, that was a dumb error on my end :sweat_smile:
Thanks!