I’m currently making a custom checkbox class using OOP. However, I’m encountering a weird issue that I’m unable to fix. When there is only 1 checkbox, it works completely fine. However, if there is more than 1 checkbox, all of the MouseButton1Click function will always be registered to the latest checkbox created.
Here’s the clip about the issue. https://streamable.com/164v3
The entire module:
CheckBoxClass = {
Checked = false,
CheckImageId = 168702841,
Adornee = nil,
CheckImage = nil,
Toggle = function(self) -- Reminder to myself: THIS IS A DAMN METHOD NOT A FUNCTION
self.Checked = not self.Checked -- ez toggling lol
if self.CheckImage and self.CheckImage:IsA("ImageLabel") then
self.CheckImage.Visible = self.Checked
end
end
}
CheckBoxClass.__index = CheckBoxClass
function MakeNewCheckbox(ParentTo, ConfigTable)
local newCheckBox = CheckBoxClass
if not ParentTo then ParentTo = workspace end -- Default ParentTo value
if not ConfigTable then ConfigTable = {} end -- Default ConfigTable values
if not ConfigTable.CheckColor3 then ConfigTable.CheckColor3 = Color3.fromRGB(0, 255, 0) end -- Default CheckColor3 value
if not ConfigTable.UDIM2Size then ConfigTable.UDIM2Size = UDim2.new(0, 50, 0, 50) end -- Default UDIM2Size value
if not ConfigTable.BorderThickness then ConfigTable.BorderThickness = 2 end -- Default BorderThickness value
if not ConfigTable.CheckboxBackgroundColor3 then ConfigTable.CheckboxBackgroundColor3 = Color3.fromRGB(255, 255, 255) end -- Default CheckboxBackgroundColor3 value
if not ConfigTable.UDIM2Position then ConfigTable.UDIM2Position = UDim2.new(0, 0, 0, 0) end
local newAdornee = Instance.new("TextButton")
newAdornee.Name = "EasyCheckbox"
newAdornee.Size = ConfigTable.UDIM2Size
newAdornee.BorderSizePixel = ConfigTable.BorderThickness
newAdornee.Text = ""
newAdornee.AutoButtonColor = false
newAdornee.BackgroundColor3 = ConfigTable.CheckboxBackgroundColor3
newAdornee.Position = ConfigTable.UDIM2Position
newAdornee.Visible = true
local newCheckImage = Instance.new("ImageLabel")
newCheckImage.Name = "CheckImage"
newCheckImage.Image = "rbxassetid://" ..newCheckBox.CheckImageId
newCheckImage.ImageColor3 = ConfigTable.CheckColor3
newCheckImage.Size = newAdornee.Size
newCheckImage.BackgroundTransparency = 1
newCheckImage.Visible = false
newCheckImage.Parent = newAdornee
newCheckBox.Adornee = newAdornee
newCheckBox.CheckImage = newCheckImage
newAdornee.Parent = ParentTo
return newCheckBox
end
function CheckBoxClass.new(ParentTo, ConfigTable)
local newCheckBox = MakeNewCheckbox(ParentTo, ConfigTable)
setmetatable(newCheckBox, CheckBoxClass)
newCheckBox.Adornee.MouseButton1Click:Connect(function()
newCheckBox:Toggle()
end)
return newCheckBox
end
function CheckBoxClass.newList(ParentTo, ConfigTable, ListConfigTable)
local CheckboxList = {}
if not ListConfigTable.AmountOfCheckbox then ListConfigTable.AmountOfCheckbox = 3 end
if not ListConfigTable.MultipleChoice then ListConfigTable.MultipleChoice = false end
if not ListConfigTable.OffsetBetweenCheckbox then ListConfigTable.OffsetBetweenCheckbox = UDim2.new(0, 0, 0, 25) end
if not ConfigTable.UDIM2Position then ConfigTable.UDIM2Position = UDim2.new(0, 0, 0, 0) end
for i = 1, ListConfigTable.AmountOfCheckbox do
local newCheckbox = MakeNewCheckbox(ParentTo, ConfigTable)
setmetatable(newCheckbox, CheckBoxClass)
CheckboxList[#CheckboxList + 1] = {newCheckbox, newCheckbox.Adornee.MouseButton1Click:Connect(function() newCheckbox:Toggle() end)}
ConfigTable.UDIM2Position = ConfigTable.UDIM2Position + ListConfigTable.OffsetBetweenCheckbox
end
return CheckboxList
end
return CheckBoxClass
If you have any clue about what went wrong, please reply to this thread.