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

How would I make a custom model remover from a character? [ANSWERED]

Hi!

So I'm trying to make it so I can use a script to remove a few models which may of been placed into the character from before.

I have these caps (Cap1, Cap2, Cap3) which may or may not be inside the character which is touching the part.

So what I need is a section of code that will scan for these models and remove them.

If you need a sample of my code then please get in touch.

Thanks :)

1 answer

✓ 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"}

-- Index variable won't be used so it's often named '_' for Lua reasons
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

0

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

0

Here is how you retrieve the character from hit, i also made few tweaks to your code: [ https://pastebin.com/5NMkW5Ek ]

0

Eyy! It worked! Thanks a lot!!

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