How do you Highlight a Part with your Mouse Inside of a Model?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve?
    When I hover over my part inside of a model I’d like to to be highlighted.
  2. What is the issue?
    When I hover over my part inside of a model it does not highlight.
  3. What solutions have you tried so far?
    I’ve looked on the DevForums & I’ve even used ChatGPT, but I’m stumped on this, and I really feel like its a simple scripting mistake…

Script that ChatGPT made that I’m currently using.

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

-- Define the tag to look for
local tagName = "Highlight"

-- Function to highlight parts (and parts within models)
local function highlightPart(part)
	-- Check if the part already has a highlight object, to avoid duplicates
	if not part:FindFirstChild("HighlightEffect") then
		-- Create a Highlight instance
		local highlight = Instance.new("Highlight")
		highlight.Name = "HighlightEffect"
		highlight.Parent = part

		-- Optionally, you can add customization here, like changing the color
		highlight.FillColor = Color3.new(1, 1, 0) -- Yellow fill
		highlight.OutlineColor = Color3.new(1, 1, 0) -- Yellow outline
	end
end

-- Function to process all parts inside a model (or model-like object)
local function processModelOrPart(object)
	if object:IsA("Model") then
		-- If it's a model, loop through all its parts
		for _, descendant in pairs(object:GetDescendants()) do
			if descendant:IsA("BasePart") then
				highlightPart(descendant)
			end
		end
	elseif object:IsA("BasePart") then
		-- If it's a part directly, just highlight it
		highlightPart(object)
	end
end

-- Function to remove highlight from a part or model
local function removeHighlightFromModelOrPart(object)
	if object:IsA("Model") then
		for _, descendant in pairs(object:GetDescendants()) do
			if descendant:IsA("BasePart") then
				local highlight = descendant:FindFirstChild("HighlightEffect")
				if highlight then
					highlight:Destroy()
				end
			end
		end
	elseif object:IsA("BasePart") then
		local highlight = object:FindFirstChild("HighlightEffect")
		if highlight then
			highlight:Destroy()
		end
	end
end

-- Function to add highlight to tagged models or parts
local function onTagAdded(object)
	processModelOrPart(object)
end

-- Function to remove highlight when the tag is removed
local function onTagRemoved(object)
	removeHighlightFromModelOrPart(object)
end

-- Highlight all currently tagged parts or models when the game starts
for _, object in pairs(CollectionService:GetTagged(tagName)) do
	processModelOrPart(object)
end

-- Connect events to respond to tag changes dynamically
CollectionService:GetInstanceAddedSignal(tagName):Connect(onTagAdded)
CollectionService:GetInstanceRemovedSignal(tagName):Connect(onTagRemoved)

I’ve added tags to the correct items, and they are inside of a vehicle, which is inside of multiple models, maybe this is the issue?? I’m pretty stumped…

1 Like

Your script works for me perfectly. What could be the issue is that there’s a limit of how many objects can be highlighted at once. Maybe you’ve reached that? If I recall correctly, it’s around ~30?

Also, within the video you showed us that the parts are in a collison group called “Highlight”, can you confirm if the parts actually have the “Highlight” tag?

I tried it with only one part and it still seems to not work, and yes I can confirm that the parts have the tag.

What’s in the screenshot is them being apart of the collision group called “Highlight”, that’s not a tag.
image

The tag property is at the bottom of the property tab.
image

My god I was using collisongroups :skull:

I added the Highlight tag to it, and it still seems to not work.

image

That’s really strange, it worked perfectly for me with 0 changes. Try running this script?

local collectionService = game:GetService("CollectionService")

for _, part in collectionService:GetTagged("Highlight") do
	print(part)
end

If nothing prints it means there’s still an issue with how you’ve tagged them

It printed fine, I only have one highlight tagged part right now

(Rewriting this to the best of my memory)

My leading theory was that it was the place where the mouse was looking @.

The mouse has nothing to do with it, your script automatically highlights everything tagged whether or not the mouse is looking at it. I don’t know what’s wrong with your firetruck as the script works for me and it’s properly tagged, I’d recommend getting a scripter to look at it personally in studio, and if they can’t manage to get it to work, having them rewrite it as sometimes chat gpt does very stupid things when generating code.

when in doubt do some print debugging it always works when Im facing a problem that I feel completely stumped on. Through print debugging you can better theorize the problem, this is what chat gpt would also recommend but i would also recommend to try to rely on chat gpt less

1 Like

I was wrong, its the actual script, and where the part is located. (I guess I wasn’t clear enough with ChatGPT because I wanted it to be highlighted when the mouse hovers over the part, guess it didnt get that.)

This script actually works perfectly fine, like I said its where the part is located, sense the part is inside of SIX different models, the script for some reason isn’t picking that up. You can see here on the left that I have one part that is open in the Workspace and another that is grouped (one time.) If I group this part again, the highlight will remove.

So did you solve the issue? (I cant read for long)

That’s weird, didn’t happen to me. Glad you found the issue though

TLDR; No, I am still mistaken on how I would get a part thats deep inside models to be highlighted.

If you want I can rewrite the script for you exactly as you want with the mouse, send me a message on dev forum

1 Like

You want to highlight a part when the mouse hovers a part with a specific tag right?

Yes, that’s exactly what Im trying to do!

Edit: You have to actually hover over the part for it to highlight unlike chatGPT’s script where it auto highlighted

local Player = game:GetService("Players").LocalPlayer
local Mouse = Player:GetMouse()

local Tag = "Highlight" --Change this to your tag

local function CheckForParts()
	local Target = Mouse.Target
	
	if Target and Target:HasTag(Tag) and not Target:GetAttribute("Highlighted") then
		Target:SetAttribute("Highlighted", true)
		
		local NewHighlight = Instance.new("Highlight")
		NewHighlight.Parent = Target
	end
end	

Mouse.Move:Connect(CheckForParts)

I found the correct reason, thank you very much for the script.

The correct reason was because the part was invisible, and for some reason roblox Highlights dont show up on invisible parts.