Is it possible to call one ClickDetector in multiple Paarts for one function

  1. I want to make it so any part in a specific folder in the workspace will allow certain functions to happen for example hovering over a part to display its health

  2. I keep getting this error
    Players.DAPANDA_KinG.PlayerScripts.LocalHighlighter:38: attempt to index nil with ‘MouseHoverEnter’
    Im assuming its not calling any clickdetector

  3. Before it was working when I had just one part but I want to incorperate many parts for one function is it possible?

This is the code im using to highlight any parts with Tag highlitable and display the Health of any parts in the game.workspace.Rocks folder

local player = game.Players.LocalPlayer

local mouse = player:GetMouse()
local StarterGUI = game:GetService("StarterGui")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Collection = game:GetService("CollectionService")

local ToolEquiped = ReplicatedStorage.Values.Equipped.Value



mouse.Move:Connect(function()

   if not mouse.Target then script.Highlight.Adornee = nil return end --Highlights

   if Collection:HasTag(mouse.Target, "Highlitable") then
   script.Highlight.Adornee = mouse.Target
   return
elseif Collection:HasTag(mouse.Target.Parent, "Highlitable") then
   script.Highlight.Adornee = mouse.Target.Parent
   return
end

script.Highlight.Adornee = nil -- Unhighlights

end)

--Health Popup

wait(3)

local MinableBlocks = game.Workspace.Rocks:GetChildren()

local ClickDetector = MinableBlocks.ClickDetector

if MinableBlocks then

ClickDetector.MouseHoverEnter:Connect(function()
   player.PlayerGui.MineGUI.RockHealth.Visible = true
   
end)

ClickDetector.MouseHoverLeave:Connect(function()
   player.PlayerGui.MineGUI.RockHealth.Visible = false
   
   end)

end

Feel free to ask for more details and anything helps thank you!

You seem to have a bit of a misunderstanding of how Tables work.

When you call

local MinableBlocks = game.Workspace.Rocks:GetChildren()

It will return a table containing all the instances inside game.Workspace.Rocks.
A table is a datatype that may contain multiple values at once, however doing an operation on the table, does not do that operation on all members of the table.

What you actually want to do, is loop over each member of the table, and then do your logic on each member individually.

This can be done by wrapping the code from line 36 and after in a for loop like so:

local MinableBlocks = game.Workspace.Rocks:GetChildren()

for _, Block in MinableBlocks do
    local ClickDetector = Block.ClickDetector

    ClickDetector.MouseHoverEnter:Connect(function()
        player.PlayerGui.MineGUI.RockHealth.Visible = true
    end)

    ClickDetector.MouseHoverLeave:Connect(function()
        player.PlayerGui.MineGUI.RockHealth.Visible = false
    end)
end

I hope this help, and I recommend you read up a bit on tables.

1 Like

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