Trying to make a thing where it gets all the click detectors in game and makes it so when you hover over them it shows the click icon but can’t figure out how to do it.
Here is the code I wrote so far - help would be appreciated
local ButtonTable = {} -- Table filled with click detectors of each of the buttons.
for i, v in pairs(workspace:GetDescendants()) do
--v.MouseClick:Connect(function()
if v:IsA("ClickDetector") then
table.insert(ButtonTable,i,v) --Inserts the ClickDetectors into ButtonTable
print(ButtonTable[i])
print("PRINTED")
end
end
local mouseIcon = "5207095807" --mouse icon
local clickIcon = "5207096357" --click detector icon
--local clickDetector = ButtonTable --click detector reference
local mouse = game.Players.LocalPlayer:GetMouse()
mouse.Icon = mouseIcon
for i, v in pairs(ButtonTable) do
ButtonTable[i].MouseHoverEnter:Connect(function()
mouse.Icon = clickIcon
end)
ButtonTable[i].MouseHoverLeave:Connect(function()
mouse.Icon = mouseIcon
end)
end
local mouseIcon = "5207095807" --mouse icon
local clickIcon = "5207096357" --click detector icon
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
mouse.Icon = mouseIcon
for i, v in ipairs(workspace:GetDescendants()) do
if v:IsA("ClickDetector") then
v.MouseHoverEnter:Connect(function()
mouse.Icon = clickIcon
end)
v.MouseHoverLeave:Connect(function()
mouse.Icon = mouseIcon
end)
end
end
Remember that this will only work in a local script because “game.Players.LocalPlayer” is referenced although that’s likely your intention.
local ButtonTable = {} -- Table filled with click detectors of each of the buttons.
local mouseIcon = "5207095807" --mouse icon
local clickIcon = "5207096357" --click detector icon
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
mouse.Icon = mouseIcon
for _, clickDetector in pairs(workspace:GetDescendants()) do
--v.MouseClick:Connect(function()
if clickDetector:IsA("ClickDetector") then
table.insert(ButtonTable, clickDetector) --Inserts the ClickDetectors into ButtonTable
print(clickDetector.Name)
clickDetector.MouseHoverEnter:Connect(function()
mouse.Icon = clickIcon
end)
clickDetector.MouseHoverLeave:Connect(function()
mouse.Icon = mouseIcon
end)
end
end
I accidentally omitted one of the events you were trying to connect in the previous script I provided, apologies for that, I’ve also kept the table creation & population in this version.
local mouseIcon = "rbxassetid://5207095807" --mouse icon
local clickIcon = "rbxassetid://5207096357" --click detector icon
Just needed to change the id’s slightly, prepend them with “rbxassetid://” and placing the script in StarterPlayerScripts or StarterCharacterScripts would be best.
Yeah, I basically just merged what you were doing before all into the same loop, so because you’re already iterating over the ClickDetector instances in workspace I just connected the same events to the same functions inside the first loop which was originally just inserting the ClickDetector instances into a table.
It doesn’t but in the 2nd script I provided it is still created and the ClickDetector instances are inserted.
for _, clickDetector in ipairs(ButtonTable) do
--do stuff
end
You can use the above to loop over it instead of looping through the entire workspace again as it’ll be more efficient to do so if you need to do it at some later point in the script.
for i, v in pairs(workspace:GetDescendants()) do
--v.MouseClick:Connect(function()
if v:IsA("ClickDetector") then
table.insert(ButtonTable,i,v) --Inserts the ClickDetectors into ButtonTable
print(ButtonTable[i])
print("PRINTED")
end
end
The variable i is incremented by 1 for each descendant iterated over (regardless of whether or not it’s a ClickDetector instance), which means that when you were calling table.insert(ButtonTable,i,v) to insert a new entry into the table i would not increment as expected by 1 for each ClickDetector instance. To fix that, removing the i argument would have sufficed as by default table.insert() (when called with only 2 arguments instead of 3) inserts the item at the end of the array.