How to disconnect a function from another function

I’m making a control point script and when players touch the point it sets up a humanoid.Died connection for that player in the part.Touched function. However, when that player leaves the point the part.TouchEnded function fires and should disconnect the humanoid.Died event for them that I set up earlier.

My issue is how would I go about disconnecting that humanoid.Died event in my part.TouchEnded function?

This is a server-sided script in ServerScriptService.

local function playerDied()
	print("died")
end

local function playerAddition(part)
	if part.Parent:FindFirstChild("Humanoid") and part.Name == "HumanoidRootPart" and part.Parent:FindFirstChild("Humanoid").Health > 0 then
		print("yes touch")
		local humanoidDeath = part.Parent.Humanoid.Died:Connect(playerDied)
	end
end

local function playerSubtraction(part)
	if part.Parent:FindFirstChild("Humanoid") and part.Name == "HumanoidRootPart" then
		print("no touch")
		--humanoidDeath:Disconnect()
	end
end

workspace.hillHitbox.Touched:Connect(playerAddition)
workspace.hillHitbox.TouchEnded:Connect(playerSubtraction)
  • I’ve tried to setup a table to store the players connection but if multiple players died it would disconnect everyone on the initial player.
  • I also tried to connect a part.TouchEnded function inside the part.Touched event, but if a player enters and leaves the part too quick then part.TouchEnded wouldn’t work.
  • I’m doing it this way because I believe this might be more efficient than checking :GetPartsInPart() every frame or so.
3 Likes

u could separately register connections for each player in a table e.g

local DiedConns = {}

local function playerAddition(part)
	if part.Parent:FindFirstChild("Humanoid") and part.Name == "HumanoidRootPart" and part.Parent:FindFirstChild("Humanoid").Health > 0 then
		print("yes touch")
        local plr = game.Players:GetPlayerFromCharacter(part.Parent)
		local humanoidDeath = part.Parent.Humanoid.Died:Connect(playerDied)
        table.insert(DiedConns[plr], humanoidDeath)
	end
end

This gives u the flexibility to disconnect specific player connections in a table

As mentioned by David, dictionaries are your friend here.
Within playerAddition you would store a value in a dictionary in the scope of the full script with the key being the humanoid, for easy access.
An example would be

local dictionary = {} -- accessible to all

function playerAddition()
    local humanoid = part.Parent.Humanoid -- whichever way you choose to get it.
    dictionary[humanoid] = humanoid.Died:Connect(functionName)
end

With this you have the value in an easy to access spot, which you can then call in a separate function, as such:

function playerSubtraction()
    local humanoid = part.Parent.Humanoid -- whichever way you choose to get it.
    if dictionary[humanoid] then -- not necessary, but its to prevent errors.
        dictionary[humanoid]:Disconnect()
    end
end

And dictionaries could be used to store basically anything, with flexibility to accommodate every player.

I honestly had no clue what dictionaries were until you explained them. After researching them for a little bit I understand them decently well. I incorporated what you had wrote into my script and it works as intended, even though it was a little broken but for another reason. My only last two questions are:

  1. Is it okay for me to set a dictionary key to nil (dictionary[humanoid] = nil) that way I don’t cause any memory leaks?
  2. Will creating a new variable (local humanoid = part.Parent.Humanoid) to reference the players humanoid in order to set a new key for the dictionary cause memory leaks?

Just make sure you properly deal with the connections before you erase them from the dictionary. I’m not exactly sure how much it’ll affect your performance at the end, but to be sure, run dictionary[humanoid]:Disconnect() BEFORE you try to erase or write over the value with the same key, as a dictionary only stores the value and a way to access the value.

If you get rid of the key or write over it, the connection still exists which could cause some weird behavior with your code if you have multiple of the same connection.

Alright thank you for letting me know. I just threw dictionary[humanoid] = nil right after the :Disconnect() so hopefully that should work. Ty again.

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