Detecting parts within models and children of other parts from workspace:GetChildren

This system will highlight parts in the workspace if they have the “Interactable” tag. This script works, however it will not detect parts that are children of another part, or are children of a model.


local workspace = game:GetService("Workspace")
local ts = game:GetService("TweenService")

local function a(object)
	local highlight = Instance.new("Highlight")
	highlight.Adornee = object
	highlight.Enabled = false
	highlight.FillTransparency = 1
	highlight.OutlineTransparency = 1
	highlight.Parent = object

	local clickDetector = Instance.new("ClickDetector")
	clickDetector.Parent = object
	clickDetector.MaxActivationDistance = 10

	clickDetector.MouseHoverEnter:Connect(function()
		highlight.Enabled = true
		local tweeninfo = TweenInfo.new(0.2, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0)
		local tween = ts:Create(highlight, tweeninfo, {OutlineTransparency = 0})
		tween:Play()
	end)

	clickDetector.MouseHoverLeave:Connect(function()
		local tweeninfo = TweenInfo.new(0.2, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0)
		local tween = ts:Create(highlight, tweeninfo, {OutlineTransparency = 1})
		tween:Play()
		highlight.Enabled = false
	end)
end

local function b()
	for _, object in pairs(workspace:GetDescendants()) do
		if object:IsA("BasePart") and object:HasTag("Interactable") and not object:HasTag("HasCD") then
			a(object)
		elseif object:IsA("BasePart") and object:HasTag("Interactable") and object:HasTag("HasCD") then
			local highlight = object:FindFirstChild("Highlight")
			local clickDetector = object:FindFirstChild("ClickDetector")
			clickDetector.MouseHoverEnter:Connect(function()
				highlight.Enabled = true
				local tweeninfo = TweenInfo.new(0.2, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0)
				local tween = ts:Create(highlight, tweeninfo, {OutlineTransparency = 0})
				tween:Play()
			end)
			clickDetector.MouseHoverLeave:Connect(function()
				local tweeninfo = TweenInfo.new(0.2, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0)
				local tween = ts:Create(highlight, tweeninfo, {OutlineTransparency = 1})
				tween:Play()
				highlight.Enabled = false
			end)
		end
	end
end

b()

workspace.ChildAdded:Connect(function(child)
	b()
end)

Any help is appreciated, thanks!

This might be able to help you

CollectionService | Documentation - Roblox Creator Hub