How to detect if the character that has fired the "touched" event has died

sorry for the vague title, i dont really know how to word it.

basically, in this game im making there are territories/flags that you can capture. im using a touched event when a nearby npc gets close and is in range to claim this territory.

i currently have a makeshift solution but i dont like it and think it can be improved.

basically: what happens when the touch event fires is that the script will wait for a bit and have visuals of the territory being claimed, and after the wait check if the character is still alive and if not it wont keep running and revert the visuals (if its still alive it will continue the script).

the issue with this is that i want the visual to immediately cancel if the character dies. i could use getpropertychanged or do a loop that checks every second but i dont know which way to go as im not too experienced, and the loop still isnt immediate.

sorry if this is confusing or a dumb question, if you need me to explain more just ask, i appreciate all the help

When the touch event fires, get the humanoid in the character that touched it and attach a humanoid.Died connection to do what you need to do

hi, quick question. will this function only run once or will it run for the main functions duration?

part.Touched:Connect(function(hit)
local hum == hit.Parent:FindFirstChild("Humanoid")
if hum then
hum.Died:Connect(function()
--code
return
end)
--code
wait(5)
--code

is this what i should do?

Yeah, that is a good start but why is there a return inside the hum.Died connection?

i want the function to stop if the character dies. see how theres a wait, and then the rest of the code. if the character dies i want it to stop waiting aka stop the function

flag.Touched:Connect(function(hit)
local Dead = false
if hit.Parent.Humanoid then
local x = hit.Parent.Humanoid.Died:Connect(function()
Dead = true
end)
x:Disconnect()
if Dead then
--RevertVisuals
else
--ContinueVisuals
end
end)

this seems to be what im looking for, ill try out the script in a few hours when im done working and tell you if its what i needed or not. thanks for the help

Adding a return inside the nested connection will not stop the script, instead when the part is touched make the hum.Died connection and when it dies you exit the function and do code

part.Touched:Connect(function(other)
   local char = other:FindFirstAncestorOfClass("Model")
   if char and char:FindFirstChild("Humanoid") then
      local hum = char.Humanoid
       --attach visuals here
      local connection = hum.Died:Connect(function()
         -- Humanoid is dead. Remove visuals here
         connection:Disconnect() -- cleanup 
      end)
   end
end)

This is just some pseudocode

i’ll also try this solution. may i ask what :Disconnect() does however? ive never used it and you refered to it as cleanup so could you give me an example of what its used for?

nevermind i looked up its definition. it seems very useful and i might have to rework some of my other scripts that should use it. thanks for giving me the idea, ill get back to you if i was able to get a solution or not soon

2 Likes

Well Basically if you don’t do : Disconnect () on any event it just runs at eats up your memory.its useful for memory storage so if you are responding to any events just do

local yournametbhcanbeanything = instance.event:Connect(function(args)
--Code Goes Here
end)

--if the Event is not needed then
yournametbhcanbeanything:Disconnect()

would i disconnect touched events too? or will that stop the touched event?

Ones the humanoids died the Died event wont fire twice, on character respawn it will just create a new humanoid, no need to return

There are two ways an event can be disconnected and i’ll briefly explain the practicality

Disconnecting an event is a good practice in order to stop unneeded listeners from using up memory for client or server

  1. Manually disconnecting, it’s the one me and @IamNewToRiblix explained, is calling :Disconnect() on a script signal. The practical uses for this is when your instance does not get destroyed via :Destroy() or any other means of getting rid of the instance frequently or does not get destroyed at all, and in this case if you have something like a one time listener for events that don’t get used afterwards then its good to disconnect them.

  2. Automatic disconnecting, when an instance is destroyed, all connections tied to that is automatically disconnected. So you dont use up memory

To save the troubles of using a connection, here is a much easier and more optimized way to run the same code. Instead of playing the visuals, then using a connection, this will be a safer way to go about it without having to make any connections per se, and the player leaves before dying, that would be an issue.

part.Touched:Connect(function(other)
   local char = other:FindFirstAncestorOfClass("Model")
   if char and char:FindFirstChild("Humanoid") then
      local hum = char.Humanoid
       --attach visuals here
      hum.Died:Wait()
      -- Humanoid is dead. Remove visuals here
   end
end)
1 Like

wouldnt this only run once? i want it to run for the duration of the visual. i already have a script that stops it if the player leaves

Yes, that would run only once. My apologies, I hadn’t seen that you were looking for it to run multiple times.

Edit: If you were meaning for it to run multiple times after the part touches the player, then yes this would run multiple times. However, if you were meaning after it touches them and they die multiple times, no this would not.