ClickDetector event won't trigger

  1. What do you want to achieve?
    I’m trying to make a system that makes a GUI show up when a player hovers over a model (currently a clickDetector is inside an attatchment inside a part inside of the model).

  2. What is the issue?
    The clickdetector event MouseHoverEnter won’t trigger when I hover my mouse over an item in workspace. The clickdetector itself does not show when hovering the mouse over it.

lp = game.Players.LocalPlayer
mouse = lp:GetMouse()

local items = {
	["item1"] = game.Workspace.Collectables.PorusMushroom:WaitForChild("Cpart").Attachment.c1,
	["item2"] = game.Workspace.Collectables.Mool:WaitForChild("Cpart").Attachment.c2
}

mouse.Move:Connect(function()
	for	_,item in pairs(items) do
		item.MouseHoverEnter:Connect(function(player)
			print("found")
		end)
	end
end)

Remove the mouse.Move event, you don’t need it. And why do you have a click detector parented to an attachment? That won’t work.

1 Like

You’re right the attatchment screws it. I must’ve mixed up proximityprompts and click detectors :sweat_smile:

How would the script run the for loop more than once if the mouse.Move is removed?
I know the current dictionary doesn’t change, but the idea is for it to append and remove items depending on if they’re in the folder.

The problem with mouse.Move is that it runs the for loop every single time the mouse is moved. So with what you wrote, if you hovered over the part, “found” is going to print like a hundred times. For what you want to do, I would use :ChildAdded() and (assuming all of your click detectors are named the same thing) :FindFirstDescendant() on the child that was added. So something like this:

local folder = workspace:WaitForChild("Collectables")

folder.ChildAdded:Connect(function(childAdded)
	local clickDetector = childAdded:FindFirstDescendant("ClickDetector")
	if clickDetector == nil then return end

	clickDetector.MouseHoverEnter:Connect(function(player)
		print("found")
	end)
end)

Although I noticed that this is in a local script, so you’ll also have to loop through the folder when the player first joins, as this only works for new things that are added. This is pretty simple too;

local folder = workspace:WaitForChild("Collectables")

for _, descendant in folder:GetDescendants() do
	if descendant:IsA("ClickDetector") then
		descendant.MouseHoverEnter:Connect(function(player)
			print("found")
		end)
	end
end

There you go. Keep in mind I haven’t tested these myself, so if you have an error let me know.

1 Like

Although keep in mind, you would create a function that handles the MouseHoverEnter event on the click detectors, this is so that you don’t have to repeat code. This is how the final script would look with the function added:

local folder = workspace:WaitForChild("Collectables")

local function mouseHoverEnter(playerWhoClicked)
	print("found")
end


for _, descendant in folder:GetDescendants() do
	if descendant:IsA("ClickDetector") then
		descendant.MouseHoverEnter:Connect(mouseHoverEnter)
	end
end

folder.ChildAdded:Connect(function(childAdded)
	local clickDetector = childAdded:FindFirstDescendant("ClickDetector")
	if clickDetector == nil then return end

	clickDetector.MouseHoverEnter:Connect(mouseHoverEnter)
end)

(also, as I was testing this, I realised :FindFirstDescendant() was giving me an error, so instead I replaced it with :FindFirstChild("ClickDetector", true), which literally does the exact same thing)

Hoped this helped, let me know if you have any questions
tay_z3r

1 Like

I inserted the script you made and it does not print. I mean the script has to recognize the player hovering whenever they hover. Right now I would guess it only prints when something is added to the folder.

I tested it for myself and found the issue. The problem is that the script executes too fast while the children of the folder take slightly longer to load in. The fix is pretty simple, just a task.wait(2). Although it’s not the most cleanest method, there’s no way to check if all of the children of an instance has loaded in, so this is the only solution.

The script now:

local folder = workspace:WaitForChild("Collectables")

local function mouseHoverEnter(playerWhoClicked)
	print("found")
end

task.wait(2)

for _, descendant in folder:GetDescendants() do
	if descendant:IsA("ClickDetector") then
		descendant.MouseHoverEnter:Connect(mouseHoverEnter)
	end
end

folder.ChildAdded:Connect(function(childAdded)
	local clickDetector = childAdded:FindFirstChild("ClickDetector", true)
	if clickDetector == nil then return end

	clickDetector.MouseHoverEnter:Connect(mouseHoverEnter)
end)
1 Like

huh. Does the folder not update (in the script)? The idea of the game is to clone things from replicated storage into the folder and then remove them as well.

Edit: Btw is there a way to make the clickdetector’s detection area bigger?

Well this is annoying. Looks like the issue is the same, the ChildAdded event fires straight after the child is added, however, that doesn’t leave any time for the click detector to load in. Exact same problem. Same solution; just a wait. Scripts looks like this now:

local folder = workspace:WaitForChild("Collectables")

local function mouseHoverEnter(playerWhoClicked, part)
	print(playerWhoClicked.Name .. " hovered over " .. part.Name)
end

task.wait(1)

for _, descendant in folder:GetDescendants() do
	if descendant:IsA("ClickDetector") then
		descendant.MouseHoverEnter:Connect(function(playerWhoClicked)
			mouseHoverEnter(playerWhoClicked, descendant.Parent)
		end)
	end
end

folder.ChildAdded:Connect(function(childAdded)
	task.wait(1)
	
	local clickDetector = childAdded:FindFirstChild("ClickDetector", true)
	if clickDetector == nil then return end

	clickDetector.MouseHoverEnter:Connect(function(playerWhoClicked)
		mouseHoverEnter(playerWhoClicked, clickDetector.Parent)
	end)
end)

Edit: Btw is there a way to make the clickdetector’s detection area bigger?

The solution for this is actually pretty simple, you just add a so called “hitbox”; an invisible and non cancollideable box around your collectable. Remember to disable anchored and weld your hitbox to a part if your collectables can move. So something like this:

image
image

1 Like

alright will test tommorow, and if it works I’ll start writing the cloning script to test how the folder behaves in the script. Should I dm you instead of posting here?

1 Like

Feel free to, I’ll help as much as I can

1 Like

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