Will this code cause any potential issues?

local Accessories = {}

for Index, Instance in next, Model:GetChildren() do
    if Instance:IsA("Accoutrement") then
        local CopiedInstance = Instance:Clone() --[[ so we wont get issues that related to "being worn by another character"
        and we can call in to destroy the previous instance without having the "property is 
        locked" issue --]]
        Instance:Destroy()
        table.insert(Accessories, CopiedInstance)
    end
end

I know the way I call my variables is pretty strangeā€”but would this code cause any future potential issues before I call it complete?

What this code does is put all of the accessories in the character into the table. So, when we want to attach these accessories into the character again after the player modelā€™s character is rebuilt, we can run this without issues:

for Index, Accessory in next, Accessories do
    Humanoid:AddAccessory(Accessory)
end

Why not just store the originals, then readd them back when needed?
As for whether any problems will arise, Iā€™m not too sure, but youā€™ll constantly have to destroy the originals, then add new ones to the player every time.

1 Like

I think the biggest issue is that you are overwriting the Instance global for that scope, which should be avoided.

That will cause several issues, such as ā€œProperty Lockedā€ issue, and a slight issue of ā€œSome Character is already wearing that itemā€

Thatā€™s why I clone them

What happens if I do that?

I donā€™t have any issues when I run the script with the ā€œInstanceā€ being the variable for that loop

It wonā€™t be a ā€˜problemā€™ unless you need to use Instance.new.
But itā€™s a bad practice and frowned upon in programming circles because it makes your code hard to edit or understand later on.

Iā€™ve used Instance.new several times in my script after that code runs and there was no issues in a result.
However, yeah sure Iā€™ll consider changing it for understanding the code alot better

The variable is only changed inside of the loop, it wonā€™t affect anything after it.

Sums up why I magically put ā€œInstanceā€ as the variable