Hey!
Sorry for the janky title, I was not sure what to call it.
I have created a basic script sing ClickDetector.MouseHover Leave/Enter to highlight objects the mouse hovers over, However, for some reason, it sometimes highlights multiple objects, even though they are all in different scripts and click detectors.
(sorry for the low res, I REALLY need to fix my obs)
Could you provide your code? We’re not really able to help right now.
Hey! Consider using Mouse.Target instead of MouseHover. Here is the documentation: Mouse | Documentation - Roblox Creator Hub
You would need to get the players mouse from a LocalScript:
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
Then you can use Mouse.Target
in your ClickDetector function. “Mouse.Target” returns the target Instance if that helps.
Going off from what @HyperFireStudios mentioned, I would agree that Mouse.Target
is the best solution for this scenario.
Using a LocalScript
, you can get the player’s mouse, as mentioned, and then use the instance and highlight it.
To simplify things, I would also recommend only using 1 LocalScript
inside of something like StarterPlayerScripts
called “HighlightScript” or something similar. This way you don’t have multiple scripts managing only 1 part.
You could then create a Folder
in the Workspace
called “HighlightParts” or something similar.
-- Highlight Script (Client)
-- Constants
local FILL_COLOR = Color3.new(1,1,1)
local FILL_TRANSPARENCY = 1
local OUTLINE_COLOR = Color3.new(1,1,1)
local OUTLINE_TRANSPARENCY = 0
local DEPTH_MODE = Enum.HighlightDepthMode.Occluded
-- Variables
local players = game:GetService("Players")
local player = players.LocalPlayer
local mouse = player:GetMouse()
local folder = workspace:WaitForChild("HighlightParts")
-- Functions
function Highlight_Part(part: BasePart)
local highlight = Instance.new("Highlight", part)
highlight.Enabled = false
highlight.FillColor = FILL_COLOR
highlight.FillTransparency = FILL_TRANSPARENCY
highlight.OutlineColor = OUTLINE_COLOR
highlight.OutlineTransparency = OUTLINE_TRANSPARENCY
highlight.DepthMode = DEPTH_MODE
mouse.Move:Connect(function()
if mouse.Target == part then
highlight.Enabled = true
else
highlight.Enabled = false
end
end)
end
function Highlight_Parts()
for _, part in pairs(folder:GetChildren()) do
if part:IsA("BasePart") then
Highlight_Part(part)
end
end
end
-- Initialize
repeat task.wait() until #folder:GetChildren() > 0
Highlight_Parts()
And if you absolutely don’t want to use Mouse.Target you can just set a variable for the instance that is being highlighted. This way there is never going to be more than 1 part highlighted. But I’d recommend the Mouse.Target version