Event disconnection not working?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    Basically if your character touches something with a humanoid it takes damage and when it dies I want it to no longer listen for touches anymore
  2. What is the issue? Include screenshots / videos if possible!
    The disconnect function just doesn’t work, I don’t know why.
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I checked the developer hub, and I tried return to stop the function but that still doesn’t work
    After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
local debounce = false
local connection
for i, v in pairs(script.Parent:GetChildren()) do
	if v:IsA("BasePart") then
		connection = v.Touched:Connect(function(other)
			if other.Parent:FindFirstChildWhichIsA("Humanoid") ~= nil then
				if not debounce then
					local humanoid = other.Parent:FindFirstChildWhichIsA("Humanoid")
					if humanoid.Health == 0 then
						local player = game.Players:GetPlayerFromCharacter(script.Parent)
						player:WaitForChild("leaderstats").Kills.Value += 1
						print("ded")
						connection:Disconnect()
						print("Won't print") -- Does print
					end
					debounce = true
					humanoid:TakeDamage(20)
					wait(0.1)
					debounce = false
				end
			end
		end)
	end
end

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

You’re overriding the connection variable each time you iterate through your loop.

So let’s say you have 3 parts in script.Parent:

PartA
PartB
PartC

When we do the first iteration, the connection variable is referencing the .Touched() connection of PartA.
When we go onto the second iteration, we reference the connection of PartB instead, and etc.

You are disconnecting the event of the most recent connection you have iterated through. In this case, it is better to store each connection in a table like so:

local connections = {}

connections[part.Name] = part.Touched

Then you can easily access the table and disconnect the relevant event.

1 Like

i don’t understand how to put that into my script

actually, forgive me. A much simpler way is this:

Instead of:

connection = v.Touched:Connect(function(other)

Replace with:

local connection = v.Touched:Connect(function(other)

Just put the

local connection

inside the for i,v in pairs cycle. You then initiate the variable with the scope of the specified BasePart. When using connection:Disconnect(), you will disconnect the connection linked to that bodypart.

1 Like

Important question do you want to disconnect ALL part connections or just the one that killed? If this is part of a character they will be reloaded, you may need to add a player attribute to track if they died by this.

local debounce = false
-- local connection --- removed here
for i, v in pairs(script.Parent:GetChildren()) do
	if v:IsA("BasePart") then
		local connection --- pasted here, important! do not assign it on the same line.
		connection = v.Touched:Connect(function(other)
			if other.Parent:FindFirstChildWhichIsA("Humanoid") ~= nil then
				if not debounce then
					local humanoid = other.Parent:FindFirstChildWhichIsA("Humanoid")
					if humanoid.Health == 0 then
						local player = game.Players:GetPlayerFromCharacter(script.Parent)
						player:WaitForChild("leaderstats").Kills.Value += 1
						print("ded")
						connection:Disconnect()
						print("Won't print") -- Does print
					end
					debounce = true
					humanoid:TakeDamage(20)
					wait(0.1)
					debounce = false
				end
			end
		end)
	end
end
1 Like

The one they just killed
placeholder

It won’t print because you disconnected the connection early.

i disconnected early yet it somehow prints

It sill doesn’t work:



local debounce = false
for i, v in pairs(script.Parent:GetChildren()) do
	if v:IsA("BasePart") or v:IsA("MeshPart") then
		local connection
	 	connection = v.Touched:Connect(function(other)
			if other.Parent:FindFirstChildWhichIsA("Humanoid") ~= nil then
				if not debounce then
					local humanoid = other.Parent:FindFirstChildWhichIsA("Humanoid")
					if humanoid.Health == 0 then
						local player = game.Players:GetPlayerFromCharacter(script.Parent)
						player:WaitForChild("leaderstats").Kills.Value += 1
						connection:Disconnect()
						print("ded")
					end

					debounce = true
					humanoid:TakeDamage(20)
					wait(0.1)
					debounce = false
				end
			end
		end)
	end
end

I can’t find a possible solution so I made it so that when something touches another and it’s dead it’s model is gone.

Why does it not work? What happens and What are you expecting to happen?

Ok when the player kills someone or something (by touching it) it dies and it’s limb fall and collapse and you gain a kill; you also gain kill when you touch a dead limb ← issue

You’ll have to set another sort of debounce for that. I would recommend a quick attribute, especially if the character is reset on death.

local debounce = false
for i, v in pairs(script.Parent:GetChildren()) do
	if v:IsA("BasePart") or v:IsA("MeshPart") then
		local connection
	 	connection = v.Touched:Connect(function(other)
			if other.Parent:FindFirstChildWhichIsA("Humanoid") ~= nil then
				if not debounce then
					local humanoid = other.Parent:FindFirstChildWhichIsA("Humanoid")
					if humanoid.Health == 0 and not humanoid:GetAttribute("claimedKill") then
						local player = game.Players:GetPlayerFromCharacter(script.Parent)
						player:WaitForChild("leaderstats").Kills.Value += 1
						connection:Disconnect()
						print("ded")
						humanoid:SetAttribute("claimedKill", true)
					end

					debounce = true
					humanoid:TakeDamage(20)
					wait(0.1)
					debounce = false
				end
			end
		end)
	end
end
1 Like

Thank you so much! This has been going on for 2 days I can finally stop worrying

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