Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why doesn't my table of click detectors work?

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.

1 answer

✓ Accepted answer

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.

2 comments

0

also i don't see reason for task.defer here since there is no loops that need to run separately from the rest of the code

0

Thanks a bunch!

This discussion is preserved as it was. New answers, comments, and votes are closed.