Humanoid.Died Event firing multiple times

Hello, I am having issues with my script which I cannot solve myself. This problem I am referring to is that the Humanoid.Died Event fires as many times as how many consecutive deaths you have avoided once you die. The script looks like this:

local players = game:GetService("Players")
local playersInGame = {}

script.Parent.ClickDetector.MouseClick:Connect(function()
	print("round started")

	for _, player in pairs(players:GetPlayers()) do
		table.insert(playersInGame, player) 
		player.Character:WaitForChild("Humanoid").Died:Connect(function() 
			print(player.Name.. " died")
			table.remove(playersInGame, table.find(playersInGame, player))
		end)
	end
	
	task.wait(5)

	print("round ended")
	
	playersInGame = {}
end)

Any help will be appreciated!

I could probably like disable the event when the round ends only if I knew how to.

1 Like

the humanoid.died function will pile up everytime the clickdetector is clicked

u might wanna do something like

local A
A = player.Character:WaitForChild("Humanoid").Died:Connect(function()
   A:Disconnect() -- so the event is no longer active
end)

i havent tested this tho

1 Like

also u might wanna use debounce to disable anymore lines getting pass the first line in mouseclick event when round is ongoing, like

local Debounce = false
script.Parent.ClickDetector.MouseClick:Connect(function()
   if Debounce == true then return end
   Debounce = true

   Debounce = false -- when round ended
end)

This actually works thank you so much!

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