I’m trying to make a system that checks when each clickdetector in a table is clicked. There are eight ClickDetectors total.
How would I get the script to initiate a function when each one is clicked?
This is the function in the script that inserts the ClickDetectors into the table:
for _,v in pairs(FuelControls:GetDescendants()) do
if v:IsA("ClickDetector") then
table.insert(ClickDetectors, v)
end
end
"FuelControls" is a model named "PLFuel". There are other models parented inside of it that are numerically named. Those models have the ClickDetectors within them (image shows only the models):
(The NumberValue in "PLFuel" is meant to increase by one whenever one of the ClickDetectors are clicked.)
Is there a way to get every ClickDetector, 1-8, in the table, store them somewhere else, and then check when each one is clicked?
You can make a loop, to check when a ClickDetector is clicked; and then add a value to PLFuel Value.
Example:
for i, clickdetector in ipairs(ClickDetectors) do
clickDetector.MouseClick:Once(function()
game.PLFuel.Value += 1 --If you have a variable with PLFuel add that.
end)
end
Edit: I’d made a mistake it’s not Connect it’s Once, I’ve fixed it thanks to @TheNexusAvenger
You can check more about ClickDetectors at this Roblox Documentation:
Assuming there is a table named ClickDetectors that is a list of the ClickDetectorss, my first thought is to use Once on all the click events to increment a counter and then call some sort of function when that total matches the total ClickDetectors. Once will automatically disconnect the event after it is used, preventing multiple clicks on a single ClickDetector from incrementing the counter.
--Initialization code above with the ClickDetectors table
local PLFuel = game.Workspace.PLFuel --Update the reference to PLFuel. It should start at 0.
for _, ClickDetector in ClickDetectors do --pairs isn't required in Luau for iterating over a table. pairs(ClickDetectors) can be used for the same results.
ClickDetector.MouseClick:Once(function()
PLFuel.Value += 1 --Increment the counter when the ClickDetector is clicked.
if PLFuel.Value ~= #ClickDetectors then return end --Only call the function if the counter has reached the total ClickDetectors.
print("All ClickDetectors clicked!")
end)
end
Be aware of Connect vs Once in this context. From the initial post:
Using Connect will make that counter increment for every mouse click. 8 mouse clicks on 1 input will appear the same as 1 mouse click on 8 inputs because it is a single counter.