Detecting when Object is duplicated in studio

I’m creating a plugin that when you dup an object with a number in its name it would go up by one (Example: PartName = "Part1 " - DupPartName = “Part2”). is there a way i could detect this?

start by detecting when the studio user holds down control, and hits d (Making a dup). Then, that dup is automatically selected in studio, meaning we can use selection service to obtain the new instance, and then name it accordingly. This should be enough to get you kicked off, lmk if you need anything else.

1 Like
local Game = game

local function OnGameDescendantAdded(NewDescendant)
	for _, Child in ipairs(Game:GetChildren()) do
		pcall(function()
			for _, Descendant in ipairs(Child:GetDescendants()) do
				if Descendant == NewDescendant then continue end
				if Descendant.Name ~= NewDescendant.Name then continue end
				local Number = string.match(Descendant.Name, "%d+$")
				if not Number then continue end
				Number += 1
				if not Number then continue end
				NewDescendant.Name = string.gsub(NewDescendant.Name, "%d+$", Number)
				break
			end
		end)
	end
end

Game.DescendantAdded:Connect(OnGameDescendantAdded)

Instead of listening for key inputs (which may not be consistent), you could instead listen for descendants to be added to the experience (game).

1 Like