Highlight Implementation Issues

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

  1. What do you want to achieve?
    I’m wanting to create a simple effect where parts are added / removed from a global highlight model with the corresponding highlight being applied / removed appropriatly.

  2. What is the issue?
    By default, it seems highlights do not apply to new parts being inserted into a model. The same also seems to apply to items being removed from a model with the highlight applied. This causes issues with the system where I’m hoping to simply reparent parts / models in and out as needed to apply the highlight effect while still remaining under the 31 instance limit.

  3. What solutions have you tried so far?
    I’ve attempted to bypass this by creating a new highlight and deleting the old one upon ChildAdded / ChildRemoved. This either results in the highlight simply not being removed, or a strange flickering effect that words fail to describe, all seemingly depending on if a task.wait() is present or not. The only solution I’ve potentially found is setting the parent of the part to nil, waiting a task.wait(), then reparenting, but that causes an undesired flicker effect and could cause potential script errors.

I am looking for ANY potential aid in this, as Highlights are still relatively new to me and seem very interesting to integrate into my game.

local highlightModel = Instance.new("Model")
highlightModel.Name = "HighlightSetup"
highlightModel.Parent = game:GetService("Workspace")

local newHighlight = script.Outline:Clone()
newHighlight.Adornee = highlightModel
newHighlight.Parent = highlightModel

local function updateHighlight(item)
	if not item:IsA("Highlight") then
		local oldHighlight = newHighlight
		newHighlight = script.Outline:Clone()
		newHighlight.Adornee = highlightModel
		newHighlight.Enabled = true
		newHighlight.Parent = highlightModel
		
		oldHighlight.Parent = nil
		oldHighlight.Adornee = nil
		oldHighlight.Enabled = false

		oldHighlight:Destroy()
	end
end

One possible solution you may want to consider is to use the descendants() method of the model to iterate through all the parts in the model and apply the highlight effect to each part individually. This way, the highlight effect will be applied to new parts as they are added to the model and removed from removed parts as they are removed from the model.

Here’s an example of how you could modify your existing code to apply the highlight effect to all the parts in the model:

local highlightModel = Instance.new("Model")
highlightModel.Name = "HighlightSetup"
highlightModel.Parent = game:GetService("Workspace")

local function applyHighlightEffect(item)
	if not item:IsA("BasePart") then
		return
	end

	local highlight = Instance.new("Highlight")
	highlight.Name = "Highlight"
	highlight.Parent = item
	highlight.Enabled = true
end

local function removeHighlightEffect(item)
	if not item:IsA("BasePart") then
		return
	end

	local highlight = item:FindFirstChildOfClass("Highlight")
	if highlight then
		highlight:Destroy()
	end
end

-- Apply highlight effect to all parts in the model
for _, item in ipairs(highlightModel:GetDescendants()) do
	applyHighlightEffect(item)
end

-- Listen for new parts added to the model
highlightModel.DescendantAdded:Connect(function(item)
	applyHighlightEffect(item)
end)

-- Listen for parts removed from the model
highlightModel.DescendantRemoving:Connect(function(item)
	removeHighlightEffect(item)
end)

This code will apply the highlight effect to all the parts in the model when the script runs, and will listen for new parts added to the model and apply the highlight effect to them as well. It will also listen for parts removed from the model and remove the highlight effect from them.

One trick I’ve found to coerce Highlight objects to properly highlight new objects is to throw a Humanoid into the model you want highlighted. This still causes some flickering, but only on the specific parts whose parents changed. The flickering is less noticeable in most cases, and it stems from the Humanoid rather than the Highlight, since it will happen even if there is no Highlight object.

After playing around some more, I’ve found that you may be able to get the Highlight to cooperate without any flicker whatsoever by even simply throwing the Humanoid straight into the workspace, but I personally would be worried about potential side effects of that. I’m not positive there are any, but it just seems like something that would have unforeseen consequences.