Highlight folder parts on mouse hover

Similar to what the title says, what I’m trying to achieve is that when the player hovers on a part in a folder, it will highlight that specific part

The issue is, it only ever highlights one part. Other parts don’t highlight whenever I hover over them. I tried naming the parts differently but that didn’t work

My script:

local RunService = game:GetService("RunService")

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

local Highlight = Instance.new("Highlight")
Highlight.FillTransparency = 1
Highlight.OutlineColor = Color3.fromRGB(255, 255, 0)

local shopIsOpen = script:WaitForChild("shopIsOpen")

RunService.Heartbeat:Connect(function()
	if shopIsOpen.Value ~= true then return end
	
	for i,v in pairs(workspace.Shop.Items:GetChildren()) do
		if mouse.Target == v then
			Highlight.Parent = v
			Highlight.Enabled = true
		else
			Highlight.Enabled = false
		end
	end
end)

I’ve seen a lot of other part highlight scripts on devforum but none of them really seem suitable for what I need (since I have a lot of parts in my folder)

1 Like
for i,v in pairs(workspace.Shop.Items:GetChildren()) do
	if mouse.Target == v then
		Highlight.Parent = v
		Highlight.Enabled = true
	else
		Highlight.Enabled = false
	end
end

Change to:

Highlight.Parent = nil
Highlight.Enabled = false
for i, v in ipairs(workspace.Shop.Items:GetChildren()) do
	if mouse.Target == v then
		Highlight.Parent = v
		Highlight.Enabled = true
	end
end

or even better:

if mouse.Target.Parent == workspace.Shop.Items then
	Highlight.Parent = mouse.Target
	Highlight.Enabled = true
else
	Highlight.Parent = nil
	Highlight.Enabled = false
end
2 Likes

this should highlight every part in the folder if you hover over only one part
edit: my bad i misinterpreted the question

local RunService = game:GetService("RunService")

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

local PartsFolder = workspace.Shop.Items

local Highlight = Instance.new("Highlight", PartsFolder)
Highlight.Enabled = false
Highlight.FillTransparency = 1
Highlight.OutlineColor = Color3.fromRGB(255, 255, 0)

local shopIsOpen = script:WaitForChild("shopIsOpen")

RunService.Heartbeat:Connect(function()
	if shopIsOpen.Value ~= true then return end
	
	if table.find(PartsFolder:GetChildren(), mouse.Target) then
		Highlight.Enabled = true
	else
		Highlight.Enabled = false
	end
end)

I’ll try this tommorow, I’ll update if it works

@lovableschoolgirl solution is fine, i’m just adding some safety checks to it because it is needed to avoid errors.

At first, it’s better to get workspace objects by using WaitForChild, because doing workspace.Shop.Items can lead to an error and break the script… sometimes scripts are loading before workspace objects, so it is not going to find things.

local Shop = workspace:WaitForChild("Shop")
local Items = Shop and Shop:WaitForChild("Items")

Secondly, you need to add a small check to the mouse target, to make sure the mouse have a target before running highlight code, without this check the script will return an error each time the mouse have no target.

if Mouse.Target then

And now your done, here is the full code

--[[ Roblox Services ]]--
local PlayerService = game:GetService("Players")
local RunService = game:GetService("RunService")

--[[ Main Variables ]]--
local Shop = workspace:WaitForChild("Shop")
local Items = Shop and Shop:WaitForChild("Items")

local Player = PlayerService.LocalPlayer
local Mouse = Player:GetMouse()

--[[ Other Variables ]]--
local Highlight = Instance.new("Highlight")
Highlight.FillTransparency = 1
Highlight.OutlineColor = Color3.fromRGB(255, 255, 0)

local shopIsOpen = script:WaitForChild("shopIsOpen")

--[[ Main Functions ]]--
RunService.Heartbeat:Connect(function()
	if shopIsOpen.Value ~= true then return end

	if Mouse.Target and Mouse.Target.Parent == Items then
		Highlight.Parent = Mouse.Target
		Highlight.Enabled = true
	else
		Highlight.Parent = nil
		Highlight.Enabled = false
	end
end)
1 Like

thanks, it works now
thank you as well @Crygen54 for making a safer version of the code

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.