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

Question about OnTouched, Models?

How would I remove a specific model OnTouched? Like if theres a Model called "Monkey" then remove?

function onTouched(hit) d.
	local d = hit.Parent:GetChildren() 
	for i=1, #d do 
		if (d[i].className == "Model") then .
			d[i]:remove() 
		end 
	end
end

script.Parent.Touched:connect(onTouched) 


4 comments

0

Are you trying to remove a model when it is touched, or when a different part is touched?

0

random period and d.

0

When the Model called Monkey, any part of Monkey (Model) is touched then it will remove Monkey (Whole model)

0

remove is depricated, use destroy.

1 answer

✓ Accepted answer

If you want to remove a model, first access the model, then remove it. If you want to do that when someone touches something, put it in that event.

Part.Touched:connect(function()
	Model:Destroy()
end)

If you want to remove the model if any part of it is touched, then you need to iterate through every part of the model:

function DestroyModelOnTouch(x,Model)
	Model=Model or x
	for _,v in pairs(x:GetChildren())do
		if v:IsA("BasePart")then
			v.Touched:connect(function()
				Model:Destroy()
			end)
		end
		DestroyModelOnTouch(v,Model)
	end
end
DestroyModelOnTouch(Model)

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