So I have this function I’ve created for making my springy animations for positioning UIs and it works flawless but the problem is that it’s still creating a new connection even if one has already been created and I tell it to return if one is but it doesn’t seem to work?
local function Spring2Part(c :RBXScriptConnection, spring1, spring2, target1, target2, frame1_info, frame2_info)
if c then return end
c = game:GetService("RunService").Heartbeat:Connect(function(dt)
spring1:update(dt, target1)
spring2:update(dt, target2)
if frame1_info.axis == "X" then
frame1_info.frame.Position = UDim2.new(spring1.pos, 0, frame1_info.frame.Position.Y.Scale, 0)
else
frame1_info.frame.Position = UDim2.new(frame1_info.frame.Position.X.Scale, 0, spring1.pos, 0)
end
if frame2_info.axis == "X" then
frame2_info.frame.Position = UDim2.new(spring2.pos, 0, frame2_info.frame.Position.Y.Scale, 0)
else
frame2_info.frame.Position = UDim2.new(frame2_info.frame.Position.X.Scale, 0, spring2.pos, 0)
end
if math.abs(spring1.pos - target1) < 0.0001 then
if c then
c:Disconnect()
c = nil
end
end
end)
end
This is how I’m calling the function, and I have a variable at the top of the script called connection2 which is set to nil
local frame1_info = {
axis = "Y",
frame = BuildingFrame
}
local frame2_info = {
axis = "X",
frame = SelectionFrame
}
Spring2Part(connection2, BuildingFrameSpring, SelectionFrameSpring, .587, 1.5, frame1_info, frame2_info)