Trying to delete second cloned model

I am trying to make something where when you click a button it clones from replicated storage and puts it in workspace but I can’t seem to find a way to delete any other cloned models after the first one. Here is my code. :


local button = ObbyStore.Activate
	
	
local Model = game.ReplicatedStorage.Model4
	
button.MouseButton1Click:Connect(function()
	
	local Clone = Model:Clone()
	Clone.Parent = game.Workspace

end)
1 Like

You can put the last cloned model into a variable and destroy it before making a new clone:

Script
local button = ObbyStore.Activate
local Model = game.ReplicatedStorage.Model4
local LastClone = nil

button.MouseButton1Click:Connect(function()
	if LastClone then
		LastClone:Destroy()
	end
	local Clone = Model:Clone()
	Clone.Parent = game.Workspace
	LastClone = Clone
end)

If you prefer to use a table to keep track of the clones, then use this script:

Script
local button = ObbyStore.Activate
local Model = game.ReplicatedStorage.Model4
local LastClones = {}

button.MouseButton1Click:Connect(function()
	for i,v in pairs(LastClones) do
		v:Destroy()
	end
	
	local Clone = Model:Clone()
	Clone.Parent = game.Workspace
	
	table.insert(LastClones, Clone)
end)
1 Like

doing this in a local script means that it will only be shown to the client only - not everyone else would be able to see it - just sayin

Use a remote event to sort that out

1 Like

It worked thank you very much! :smile: