Why does it create a connection even when there is one already?

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)
1 Like

So are you sure the function is being called just once?

At the very beginning of the function, just above where you have ‘if c then return end’
put a line
print("this is c : ",c)

Check if the function is being called more than once, and you can tell if anything is in c or not.

1 Like

Okay for some background I run the Spring2Part function two times when two different buttons are being clicked, so how I fixed it was I added another parameter inside of the function call super_secret_code

And when I called both functions I passed along just a string identifier and it works flawless!

local codes = {}
	
local function Spring2Part(spring1, spring2, target1, target2, frame1_info, frame2_info, super_secret_code)
	
	if codes[super_secret_code] then
		return
	end

	local c
	
	codes[super_secret_code] = true
	
	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
				codes[super_secret_code] = nil
				c:Disconnect()
				c = nil
			end
		end
		
	end)
end

Call 1

Spring2Part(BuildingFrameSpring, SelectionFrameSpring, .587, 1.5, frame1_info, frame2_info, "blah")

Call 2

Spring2Part(BuildingFrameSpring, SelectionFrameSpring, 1, .893, frame1_info, frame2_info, "blah")

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.