Attempt to index nil with 'connect'

Hello! I’m trying to make a custom mouse icon that rotates when you can interact with a click detector. So I did that by leaving the original mouse icon invisible and creating two fake mouses using image labels, with one of them stopped and the other one spinning.

local normalMouse = script.Parent:WaitForChild('MouseSpinning').FakeMouse
local movingMouse = script.Parent:WaitForChild('MouseSpinning').FakeMouseMoving

And then I made this…

		cd.MouseHoverEnter:Connect(function()
			normalMouse.ImageTransparency = 1
			movingMouse.ImageTransparency = 0
		end)
		cd.MouseHoverLeave:Connect(function()
			normalMouse.ImageTransparency = 0
			movingMouse.ImageTransparency = 1
		end)

But there’s a problem…
At first cd was a table with all the click detectors, but it wasn’t the best method since some click detectors would be added later, and it would be a mess to make all of them with a code that animates the “mouse icon”. So I tried this:

local cd = game.Workspace:GetDescendants()

for i, v in pairs(cd) do
	if v:IsA("ClickDetector") then
		cd.MouseHoverEnter:Connect(function()
			normalMouse.ImageTransparency = 1
			movingMouse.ImageTransparency = 0
		end)
		cd.MouseHoverLeave:Connect(function()
			normalMouse.ImageTransparency = 0
			movingMouse.ImageTransparency = 1
		end)

	end
end

But as you can imagine, it doesn’t work…
What could I do?
(the camera is locked to first person, so the only problem here is to get all the click detectors, even if them are added later)

I would do this…
In a local script, have a loop, while do, or using render step, etc…
Each pass of that loop, get the instance the mouse is pointed at, and check if it has a click detector as its child
if yes, make your icon the spinning one,
if no, make your icon the non spinning one

keep a variable holding the last instance your mouse was over, and only do the check if the current instance the mouse is looking at is different than the last instance it was looking at

https://developer.roblox.com/en-us/api-reference/property/Mouse/Target

Hope this helps

1 Like

So, Are you getting the mouse position and putting the labels at the exact mouse position? If you don’t, so probably you wouldn’t see the new mouse icon spinning.

1 Like

No, the game is in first person, the icon is locked in the middle…

Here is what I used in my game, back when filtering enabled was new and click detectors were not working.
Put in a local script

GrabIcon = "rbxasset://textures/DragCursor.png"

local lastPart = nil
local function onRenderStep(deltaTime)
	if Mouse.Target ~= lastPart then
		local icon = ""
		local tool = Player.Character:FindFirstChildOfClass("Tool")
		if Mouse.Target and not tool and Mouse.Target:FindFirstChild("ClickDetector") then
			icon = GrabIcon
		end

		lastPart = Mouse.Target
        Mouse.Icon = icon
	end
end
RunService.RenderStepped:Connect(onRenderStep)
1 Like

Okay, this code should work…

What is the Parent of LocalScript?

1 Like

cd is a table
you should use this instead

v.MouseHoveEnter:Connect(function())

by doing this:

you try to index “MouseHoverEnter” of the table instead of the ClickDetector itself

3 Likes

It is PlayerGui
30 cool letters :roll_eyes:

As @Lielmaster told, you trying to use those events in the table “cd”, just change it to “v”

1 Like