Save values in functions created in loop

I’m making a plugin that creates waves. And script creates multiple buttons with loop like

for i,wave in ipairs(waves) do
    button = ...
    button.MouseButton1Click:Connect(function()
        (something)
    end)
end

but functions cant save with what element event was.

I fount a way:

function savevalue(val,button)
    local value = val
    button.MouseButton1Click:Connect(function()
        print(value)
    end)
end
for i,wave in ipairs(waves) do
    button = ...
    savevalue(i,button)
end