Hello. I'm trying to make a script that changes the ClickDetector's mouse icon when the player hovers on a part that can be clicked, and when they stop hovering on it, it changes back to default.
However, it doesn't work with a table, and I have no idea on how to fix it because I don't really use tables that often.
The error: attempt to index nil with 'Connect'
task.defer(function() local mouse = game.Players.LocalPlayer:GetMouse() mouse.Icon = 'http://www.roblox.com/asset/?id=9824394203' local clickDetector = {game.Workspace.Part.ClickDetector, game.Workspace.Part2.ClickDetector} clickDetector.MouseHoverEnter:Connect(function() mouse.Icon = 'http://www.roblox.com/asset/?id=4595124035' print("Cursor Changed!") end) clickDetector.MouseHoverLeave:Connect(function() mouse.Icon = 'http://www.roblox.com/asset/?id=9824394203' print("Default Cursor.") end) end)
The script is in Startergui btw.
the correct way is to loop through each click detector in the table, looks like this:
local clickDetectors = {game.Workspace.Part.ClickDetector, game.Workspace.Part2.ClickDetector} for index, clickDetector in clickDetectors do print(index, ";", clickDetector) clickDetector.MouseHoverEnter:Connect(function() mouse.Icon = 'http://www.roblox.com/asset/?id=4595124035' print("Cursor Changed!") end) clickDetector.MouseHoverLeave:Connect(function() mouse.Icon = 'http://www.roblox.com/asset/?id=9824394203' print("Default Cursor.") end) end
your output for this exact code should be
--> 1 ; ClickDetector --> 2 ; ClickDetector
you should of course remove the print it's just to demonstrate how looping works.