Connection Doesn't Disconnect

I made a tool that drops pianos from the sky, they deal damage if they hit a player and then they explode. It works perfectly, except for the fact that every time that I drop two pianos around the same time interval, if they touch each other the connection for some reason never disconnects until the piano explodes…

Here’s the code

	Connection = bombClone.Touched:Connect(function(hit)
		local Player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
		
		if Player and Player.Team ~= user.Team then
			Connection:Disconnect()
			
			hit.Parent:FindFirstChildOfClass("Humanoid"):TakeDamage(35)
			explode(bombClone, user)
		elseif not Player then 
			Connection:Disconnect() 
			print(hit)
			
			explode(bombClone, user)
		end
	end)
1 Like

You can also simply do

bombClone.Touched:Wait()

if Player and Player.Team ~= user.Team then
	hit.Parent:FindFirstChildOfClass("Humanoid"):TakeDamage(35)
	explode(bombClone, user)
elseif not Player then
	print(hit)		
	explode(bombClone, user)
end

I’m sorry but how is this going to work at all? I can’t even access what was touched and also I’m not relatively sure if this would fix my issue…

Can I see where the connection variable is stored?

Also this should work

local hit = bombClone.Touched:Wait()

A case I see where it wouldn’t disconnect is if Player and Player.Team == user.Team is true. Perhaps it can be fixed if you move Connection:Disconnect() outside your if statement:

--above code
Connection:Disconnect()
if Player and Player.Team ~= user.Team then
--rest of the code

and remove it from inside the if statement cases.

why not use Once

If bombClone.Touched:Once(function(hit)
		local Player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
		
		if Player and Player.Team ~= user.Team then
			Connection:Disconnect()
			
			hit.Parent:FindFirstChildOfClass("Humanoid"):TakeDamage(35)
			explode(bombClone, user)
		elseif not Player then 
			Connection:Disconnect() 
			print(hit)
			
			explode(bombClone, user)
		end
end```
1 Like

This ended up working, but why does this work over disconnecting events?

Because Once only does it once then disconnects itself

1 Like

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