How to remove certain string from an instance name

Hello everyone,

I’m making a system where if you have more then one of the same item the name of the item becomes
(ToolName)_(Amount of the same tool in the inventory) for example Axe_1.

I want to remove the _1 at the end of the tool’s name without removing any character from the tool’s name.

I tried string.sub(Tool.Name,1,string.len(Tool.Name) - 2) and it works if the Amount of items is less then 9 but it wouldn’t delete the full thing if for example I had Axe_10. Is there a better way to do it?

Thank you.

local function itemtitle(item, amount)
	local name = item.Name:split("_")[1]
	if amount == 1 then
		return name
	elseif amount <= 0 then
		return false
	else
		return name .. " (x"..amount..")"
	end
end

concatenate.

local itemNum = 2
local CutToolName = string.sub(Tool.Name, 1, string.len(Tool.Name) - 2)
local Solution = CutToolName .. "_" .. itemNum

print(Solution)

I have a question to ask if you don’t mind, what is Amount?

The amount of the item you have in your inventory. I didn’t understand the question fully. What are the items actually called? Axe_1, Axe_2, etc?

No my question was how I could remove _(Number of items) regardless of the number since if it has more then 1 digit then it would keep the _ in the tool’s name

You should use string.split:

Tool.Name:split("_")[1] --> Axe

If you want to keep the _, you should use gsub:

Tool.Name:gsub("%d", "") --> Axe_
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.