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)
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)