Highlight part when hovered with a ClickDetector

Recently I saw a post by Katrist where they were able to create highlighted parts when a user hovers over a click detector using collection service so I added onto it to make it more usable with multiple other click detectors. I thought to share it to the forum since it may be useful to some, also feel free to update it and make it better!

To use it all you have to do is create a tag named “Highlightable” and then tag the part. You can also add styles if you like by adding a string attribute in the click detector.

--//Services
local CollectionService = game:GetService("CollectionService")

--//Functions

local function CreateHighlight(part,style)
	local highlight = Instance.new("Highlight")
	
	if not style then
		highlight.FillTransparency = 0
		highlight.OutlineColor = Color3.new(1, 1, 1)
		highlight.Parent = part
	end
	
	if style == "White" then
		highlight.OutlineTransparency = 0
		highlight.FillTransparency = 1
		highlight.DepthMode = "Occluded"
		highlight.OutlineColor = Color3.new(1, 1, 1)
		highlight.Parent = part
	end

	if style == "Red" then
		highlight.OutlineTransparency = 1
		highlight.OutlineColor = Color3.new(1, 0, 0.0156863)
		highlight.Parent = part
	end

end

local function DestroyHighlight(part)
	if part:FindFirstChild("Highlight") then
		part.Highlight:Destroy()
	end
end


local function InitializeHighlightable(part)
	local prompt = part.ClickDetector

	prompt.MouseHoverEnter:Connect(function()
		if not prompt:GetAttribute("Style") then
			CreateHighlight(part)
		else
			CreateHighlight(part,prompt:GetAttribute("Style"))
		end
		
	end)

	prompt.MouseHoverLeave:Connect(function()
		DestroyHighlight(part)
	end)
end

CollectionService:GetInstanceAddedSignal("Highlightable"):Connect(InitializeHighlightable)

for i, highlightable in CollectionService:GetTagged("Highlightable") do
	task.spawn(InitializeHighlightable, highlightable)
end

Test Place:
highlighters.rbxl (55.8 KB)

5 Likes