The function below is called every time a child change on a GUI frame is detected. It is used as a refresh that checks any new buttons or any buttons removed in the change, and then if any of those buttons is clicked, something happens. But, it has a problem of not forgetting the function called before a new one was, and a click on a button that was there before then registers 2 clicks, because the button was there when both functions got called.
Therefore if not already obvious, there are 2 functions alive that operate the same button, which is a problem.
I don’t know how I could kill the old function, but here is the code for those who may have an idea of how to do so:
local function refreshChildren()
for i, v in pairs(script.Parent:GetChildren()) do
if v:IsA("ImageButton") then
if v.Name ~= "ADDSITE" then
v.MouseButton1Click:Connect(function()
if InEditMode then
v:Destroy()
else
print("lolek")
end
end)
end
end
end
end
You could add the functions to a table, then loop through that table and disconnect them every time you refresh.
local ButtonFunctions = {}
local function refreshChildren()
for i, v in pairs(ButtonFunctions) do
v:Disconnect()
v = nil
end
for i, v in pairs(script.Parent:GetChildren()) do
if v:IsA("ImageButton") then
if v.Name ~= "ADDSITE" then
local buttonFunction = v.MouseButton1Click:Connect(function()
if InEditMode then
v:Destroy()
else
print("lolek")
end
end)
table.insert(ButtonFunctions, buttonFunction)
end
end
end
end
If this does not work, try making the button’s function into a seperate function and calling that on the button click, like this.
local function ButtonFunctionExample()
if InEditMode then
v:Destroy()
else
print("lolek")
end
end
--This is outside of the function for the example,
-- put this in the same place that it is above.
local buttonFunction = v.MouseButton1Click:Connect(ButtonFunctionExample)