Connections remain active after being disconnected on the client, fires multiple times the more I reset my character

Hello everyone,

I am having trouble with cleaning a connection, every time I reset the remote event fires more times. It seems like I am not doing a good job cleaning the connection as I should.

Every time I jump the client fires a remote event to the server, but the more times I respawn the more times the event get fired per jump. This is a local script in a ScreenGui which has resetOnSpawn = false.

-- jump detection is more consistent on the client
local jumpConnection = nil

local function cleanup()
	if jumpConnection then
		jumpConnection:Disconnect()
		jumpConnection = nil
		warn("jump connection cleaned")
	end
end

-- initial connection
jumpConnection = hum.Jumping:Connect(function(active)
	playerJumped:FireServer(active)	
end)

-- cleanup
Players.LocalPlayer.CharacterAdded:Connect(function(character)
	print("Player respawned")
	cleanup()

	-- create new connection
	jumpConnection = character:WaitForChild("Humanoid").Jumping:Connect(function(active)
		playerJumped:FireServer(active)	
	end)
	warn("started new jump connection")
end)

Players.LocalPlayer.CharacterRemoving:Connect(function()
	cleanup()
end)

Is it just firing twice?
That’s probably because Humanoid:Jumping has an active parameter and it fires twice, once with active = true and then soon after with active = false.

Edit: Since you said it’s happening more each time you respawn then I would make sure you don’t have a duplicate of this anywhere with resetOnSpawn = false.

I copied your script and put it in a fresh project and after respawning multiple times it’s still firing the expected number of times (twice once with active == true, once with active == false)

Does cleanup get called? I remember when I had deferred signals a while ago where events like CharacterRemoving don’t get called. Might’ve been fixed now

local players = game:GetService("Players")
local player = players.LocalPlayer

local function OnCharacterAdded(character:Model)
	local humanoid = character:WaitForChild("Humanoid")
	humanoid.Jumping:Connect(function(isActive:boolean)
		-- fire server
	end)
end

player.CharacterAdded:Connect(OnCharacterAdded)
if player.Character then
	OnCharacterAdded(player.Character)
end

Thanks for taking the time to test it out, I did the same and tested it on a new project and I was surprised that it is working correctly, but I checked with Ctrl+Shift+F in my main game and I don’t have anything else firing this event except this local script in a resetOnSpawn = false ScreenGui. I am still not sure why is this happening.

I will keep you all updated in case I figure it out. I just hope this is not a studio bug of some sort.

Yes it gets called and prints jump connection cleaned. As @CookieAroundTheBend have pointed out, the code seems to be working fine in a new project. So it might not be a code issue after all. I still have no clue why this is happening.

Is the GUI just in starterGUI and not something that would get cloned from somewhere else? You should check the Player.PlayerGUI through the explorer while running the game after respawning a couple of times to see if multiple instances are there.

1 Like

You absolutely nailed it! That’s exactly what’s happening lol. I had a script during testing that would create a new ScreenGui and when I finished the proper Gui I just started to clone it completely overlooking the fact that it has a script. I instantly thought of that script the moment you said it might be cloned from somewhere else. Cheers!