Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why doesn't my table of click detectors work?

Asked by 2 years ago

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

Log in to vote
0
Answered by
imKirda 4491 Moderation Voter Community Moderator
2 years ago
Edited 2 years ago

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.

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 imKirda 4491 — 2y
0
Thanks a bunch! boijuztgotrecked4 7 — 2y
Ad

Answer this question