✓ Accepted answer
You would need to have basic knowledge of iteration loops. You need to GetChildren of the character, iterate through each one of them and check if it's name is not "Cap1" or "Cap2" or ...
Here is how i would implement it:
local Character = [[ Your Character Model ]]
local RemoveableCaps = {"Cap1", "Cap2", "Cap3"}
for Index, Child in ipairs(Character:GetChildren()) do
if table.find(RemoveableCaps, Child.Name) then
Child:Destroy()
end
end
[RemoveableCaps] table stands for names, each cap that has it's name inside of the table will be removed from the character. When the loop begins, it checks if name of each cap has been found inside of the table and if so, destroy it. It does that using table.find which returns position of element inside of array or nil if it has not been found.
Here is Lua wiki on iteration loops, you should read it, it explains it shortly and well.
In case if these caps are not parented to the Character but for example to his leg or something, you can use GetDescendants which returns all children and it's children and their children (and so on) of the Instance in a table, the syntax won't differ:
local Character = [[ Your Character Model ]]
local RemoveableCaps = {"Cap1", "Cap2", "Cap3"}
for _, Descendant in ipairs(Descendant:GetChildren()) do
if table.find(RemoveableCaps, Descendant.Name) then
Descendant:Destroy()
end
end
Sources: GetChildren, GetDescendants, Iteration loops
3 comments
Hey, so I put the script inside of an onTouch function, and I've tried to mess around with the 'local Character =' but can't seem to get it to work. Here's a pastebin of what I have so far: https://pastebin.com/5ajcsMkQ
Here is how you retrieve the character from hit, i also made few tweaks to your code: [ https://pastebin.com/5NMkW5Ek ]
Eyy! It worked! Thanks a lot!!