Need help with roblox name collisions

Hello, I haven’t found any topic about this so i decided to post one, if one already exists please tell me and i will remove this post.
I have found an issue while changing values of the “Highlight” property. Instead of using proximity prompt I use the highlight property and i change its outline transparency when the players mouse comes in contact with a ClickDetector thats inside my model or part. The issue is that for some reason, even after chaning the Highlights name, there seems to occur a “Name Collision” what i mean by that is that when the players mouse hovers over the ClickDetector, instead of only highlighting that specific part, random parts that also have a “Highlight” property highlight as well. I’ve tried changing the highlight names but that doesn’t seem to help at all. Sometimes everything works fine and sometimes different things highlight. Here’s the script im using.

local Highlight = Instance.new("Highlight")
Highlight.Name = "High2"
Highlight.Parent = model
Highlight.OutlineTransparency = 1
Highlight.FillTransparency = 1

cd.MouseHoverEnter:Connect(function()
	local Tweeng = {}
	Tweeng.OutlineTransparency = 0
	
	local tweenI = TweenInfo.new(0.5, Enum.EasingStyle.Quad)
	tweenserv:Create(Highlight,tweenI,Tweeng):Play()
end)

cd.MouseHoverLeave:Connect(function()
	local Tweenh = {}
	Tweenh.OutlineTransparency = 1
	
	local tweenIn = TweenInfo.new(0.5, Enum.EasingStyle.Quad)
	tweenserv:Create(Highlight,tweenIn,Tweenh):Play()
end)

Any help will be appreciated,
Thank you for your time.

It would be hard to find the exact issue without more information, but you could circumvent this by only creating a single Highlight, and then parenting it to the ClickDetector you are currently highlighting. Since you will only ever be able to hover over one object at a time, this shouldn’t be noticable, but it would increase performance, and likely fix your issue.

I would recommend to make sure that you are activating/de-activating Highlights as there is a maximum allowed amount of Highlights that can be active at one time. On MouseHoverEnter Set Enabled as true and on MouseHoverLeave, after the tween set Enabled to false.

This hard limit sometimes may ignore the Enabled property being false, so your best bet is actually creating and destroying a Highlight Instance on hover. Creating an Instance has some overhead, but at the moment this is the only way to reliably do this with the cap of 31 Highlights.

It’s also worth noting that we do not know the structure of your Model, attaching a Highlight to a Model will affect anything parented to it.

local highlight: Highlight?
local info = TweenInfo.new(0.5, Enum.EasingStyle.Quad)

cd.MouseHoverEnter:Connect(function()
    if highlight then
        return
    end

    highlight = Instance.new("Highlight")
    highlight.OutlineTransparency = 0
    highlight.FillTransparency = 0
    highlight.Parent = model

    tweenserv:Create(highlight, info, {
        OutlineTransparency = 1,
        FillTransparency = 1
    }):Play()
end)

cd.MouseHoverLeave:Connect(function()
    if highlight then
        local tween = tweenserv:Create(highlight, info, {
            OutlineTransparency = 0,
            FillTransparency = 0
        })

        tween:Play()
        tween.Completed:Wait()
        highlight:Destroy()
        highlight = nil
    end
end)

This was written on mobile as I’m currently flying. Sorry if there’s any mistakes, but hopefully this helps!

1 Like

So I tried this in studio and it’s pretty clever to do that but first i dont really understand what this means

local Highlight: Highlight?

And also the script manages to delete the highlight once but after that it keeps on making more and more highlights

Thank you for replying to my question, I will definetily try that!

The syntax you are unsure about is type annotation.
You are basically telling the script editor that the variable Highlight is a Highlight object. The questionmark indicates that the variable may be nil. So you are telling this script editor “This variable is a highlight, but may sometimes be nil”. Which is useful for autocompletion and ensuring the code is safe.

1 Like

I didn’t catch that! Sorry I was on a plane when I wrote that. You will need to have a debounce on the MouseHoverLeave.

local debounce = false
cd.MouseHoverLeave:Connect(function()
    if highlight and not debounce then
        debounce = true
        local tween = tweenserv:Create(highlight, info, {
            OutlineTransparency = 0,
            FillTransparency = 0
        })

        tween:Play()
        tween.Completed:Wait()
        highlight:Destroy()
        highlight = nil
        debounce = false
    end
end)

This may not be the best approach as this does make hovering/un-hovering quickly to not immediately update. Having a way to interrupt the debounce should be considered.

1 Like