Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I delete a previous clone?

I'm trying to make a character creator and I have a GUI that allows players to choose a hair accessory to wear. Upon selection, an accessory is cloned unto players' character. I would like for players to be able to cycle through accessories, thus deleting the previously worn accessory and then cloning a new one. How do I identify and delete the previous clone within the script?

If any additional clarification is needed, please don't hesitate to ask me. All help is appreciated!

The script below is an idea of how I thought it'd work, but it didn't.

local clone = nil

function updateMidBox()
	if clone ~= nil then --If a clone exists, delete it
		clone:Destory()
	end
	if numb.Value > 0 then
		midBox.Text = ("Hair: "..hairFolder[numb.Value].Name)
		addAccoutrement(char, hairFolder[numb.Value])
		local clone = hairFolder[numb.Value] --The new hair piece is defined as the "clone"
	else
		midBox.Text = ("Hair: None")
	end
end

2 answers

✓ Accepted answer

clone:Destroy() should be it, however you wrote clone:Destory() at Line 5.

I can't test this out rn, but I think this works

local ReplicatedStorage = game:GetService("ReplicatedStorage")

function updateMidBox()
	local addAccessory = ReplicatedStorage:WaitForChild("AddAccessory")

    	if numb.Value > 0 then
        	midBox.Text = ("Hair: "..hairFolder[numb.Value].Name)
        	local accessory = hairFolder[numb.Value]

		addAccessory:Fire(accessory)
    	else
        	midBox.Text = ("Hair: None")
    	end
end

Server Script

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local addAccessory = ReplicatedStorage:WaitForChild("AddAccessory")

addAccessory.OnServerEvent:Connect(function(player, hat)
	local Character = player.Character or player.CharacterAdded:Wait()
	local Humanoid = Character:FindFirstChildWhichIsA("Humanoid")
	
	local accessory = hat:Clone()
	
	local AccessoryInPlayer = Character:FindFirstChild(accessory.Name)	

	if not AccessoryInPlayer then
		Humanoid:AddAccessory(accessory)
	else
		AccessoryInPlayer:Destroy()
	end
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.