Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I delete a previous clone?

Asked by 3 years ago

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

Log in to vote
1
Answered by 3 years ago

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

Ad
Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

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)

Answer this question