Storing clickdetectors in a table

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
1 Like
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.

1 Like

Yeah, it’s in a local script so that isn’t the issue.

1 Like
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.

1 Like

Can you explain this script so I know what I am doing?

Also the mouse itself doesn’t seem to change? It’s in StarterGui

image

1 Like
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.

https://gyazo.com/ca1c74e3cdda6246786a8f397dc49228

1 Like

Thanks for everything, can you explain what you fixed?

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.

Does it actually use the table anymore?

1 Like

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.

Yeah that makes a bit more sense when I think about it, cheers.

1 Like

Regarding the issue with the original 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.

Alright thanks for that, I was not too sure about how to do the table.

Do you have any clue how I would get this to work with streaming enabled?