How to print once when hum died

i want to make that if i kill someone, i get only 1 kills and died player gets 1 death value However if i kill someone, i get more than 10 kills at once and died player gets more than 10 deaths Furthermore, there is a code which prints hum died and that code prints about 14 times at once

	local Debounce = false
					
				        EnemyCharacter.Humanoid.Died:Connect(function()
						print(EnemyCharacter.Name.." is Died by "..Player.Name.." with Sword")
						if game.Players:FindFirstChild(EnemyCharacter.Name) then
							if Debounce == false then
								Debounce = true
								local enemy = game.Players:FindFirstChild(EnemyCharacter.Name)
								local kills = Player:FindFirstChild("leaderstats"):FindFirstChild("Kills")
								local current_kills = Player:FindFirstChild("Current Kills")
								local death = enemy:FindFirstChild("leaderstats"):FindFirstChild("Death")

								kills.Value += 1
								current_kills.Value += 1
								death.Value += 1
								task.wait(3)
									Debounce = false
							else
								warn(EnemyCharacter.Name.." is not a vaild member of Players")
								end
								end
							end)

this is a module script

Is this Humanoid.Died Connection inside a function? If it is, you might be running the function multiple times, which in consequence creates multiple connections.

2 Likes

i will try it later

this will be blur

it keeps printing “hum died” 14 times at once when hum died

Is that the only script that adds value? What is an EnemyCharacter?

1 Like

This is not the entire script, Could you pls provide a bit more of the code, Is this inside a fucntion?, If so, when do you call the function? is it in a loop?, is suspect that you are running this muliple times?

1 Like
--|| Services ||--
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage"):WaitForChild("Sword Combat1")
local TweenService = game:GetService("TweenService")
local Debris = game:GetService("Debris")
local ServerStorage = game:GetService("ServerStorage")

--|| Variables ||--
local RemoteFolder = ReplicatedStorage.Remotes

--|| Remotes ||--
local ClientRemote = RemoteFolder.SwordClient
local CameraRemote = RemoteFolder.CameraRemote
local AnimationRemote = RemoteFolder.AnimationRemote

--|| Modules ||--
local GlobalFunctions = require(ReplicatedStorage.GlobalFunctions)
local Ragdoll = require(ServerStorage.Modules.Ragdoll)

local BlurData = {Size = 30; Length = .25}

local DamageHandler = {

	TakeDamage = function(Character,Inflicted,ToolName,Damage)		
		local Player = Players:GetPlayerFromCharacter(Character)
		local Humanoid = Character:FindFirstChild("Humanoid")

		if Inflicted.Health > 0 and Humanoid.Health > 0 then
			local EnemyCharacter = Inflicted.Parent
			local EnemyPlayer = Players:GetPlayerFromCharacter(EnemyCharacter)

			local State = EnemyCharacter:FindFirstChild("BlockHits")
			local Feint = EnemyCharacter:FindFirstChild("Feint")

			if Feint then
				if Player then
					AnimationRemote:FireClient(Player,nil,"StopAllAnimations")
					AnimationRemote:FireClient(Player,"swordazed","Play")
					delay(1.25,function()
						AnimationRemote:FireClient(Player,"swordazed","Stop")
					end)
					GlobalFunctions:AddValue("BoolValue", "Stun", false, Character, 1.5)
				end	
			end
			
			if State then
				local BlockHits = EnemyCharacter:FindFirstChild("BlockHits")				

				if BlockHits.Value >= 1 then
					delay(.175,function() GlobalFunctions:DamageIndicator(EnemyCharacter,"-"..Damage/3); ClientRemote:FireAllClients{Module = "SwordVFX"; Function = "Block"; Victim = EnemyCharacter} end)

					Inflicted.Health = Inflicted.Health - Damage/3
					
					print("Enemy : "..Inflicted.Parent.Name.."(Blocking)", "Fighter : "..Player.Name.." (Sword)")
					
					EnemyCharacter.Humanoid.Died:Connect(function()
						print(EnemyCharacter.Name.." (Blocking) is Died by "..Player.Name.." with Sword")
						if game.Players:FindFirstChild(EnemyCharacter.Name) then
							local enemy = game.Players:FindFirstChild(EnemyCharacter.Name)
							local kills = Player:FindFirstChild("leaderstats"):FindFirstChild("Kills")
							kills.Value += 0.07142857142
						else
							warn(EnemyCharacter.Name.." is not a vaild member of Player")
						end
					end)
					
					end
				

				if BlockHits.Value <= 0 then
					if EnemyPlayer then
						AnimationRemote:FireClient(EnemyPlayer,"swordazed","Play")
						delay(1.25,function()
							AnimationRemote:FireClient(EnemyPlayer,"swordazed","Stop")
						end)
					else
						local AnimTing = Inflicted:LoadAnimation(ReplicatedStorage.Animations.sword.swordazed)
						AnimTing:Play()
						delay(1.25,function()
							AnimTing:Stop()
						end)
					end

					GlobalFunctions:AddValue("BoolValue", "Choosimg", false, EnemyCharacter, .7)
					GlobalFunctions:AddValue("BoolValue", "Stun", false, EnemyCharacter, 1.5)

					BlockHits:Destroy()

					GlobalFunctions:AddValue("BoolValue", "Stun", false, EnemyCharacter, 1.5)
					EnemyCharacter.HumanoidRootPart.Anchored = true	
					coroutine.resume(coroutine.create(function()
						wait(1.5)
						EnemyCharacter.HumanoidRootPart.Anchored = false
					end))

					delay(.175,function() ClientRemote:FireAllClients{Character = Character; Module = "SwordVFX"; Function = "GuardBreak"; Victim = EnemyCharacter} end)					
				end	
			else
				if EnemyCharacter:FindFirstChild("Choosing") == nil and EnemyCharacter:FindFirstChild("Feint") == nil then
					delay(.175,function() Inflicted.Health = Inflicted.Health - Damage; GlobalFunctions:DamageIndicator(EnemyCharacter,"-"..Damage) end)
					print("Enemy : "..EnemyCharacter.Name, "Fighter : "..Player.Name.." (Sword)")
					
					local Debounce = false

					EnemyCharacter.Humanoid.Died:Connect(function()
						print(EnemyCharacter.Name.." is Died by "..Player.Name.." with Sword")
						if game.Players:FindFirstChild(EnemyCharacter.Name) then
							if Debounce == false then
								Debounce = true
								local enemy = game.Players:FindFirstChild(EnemyCharacter.Name)
								local kills = Player:FindFirstChild("leaderstats"):FindFirstChild("Kills")
								local current_kills = Player:FindFirstChild("Current Kills")
								local death = enemy:FindFirstChild("leaderstats"):FindFirstChild("Death")

								kills.Value += 1
								current_kills.Value += 1
								death.Value += 1
								task.wait(3)
								Debounce = false
							else
								warn(EnemyCharacter.Name.." is not a vaild member of Players")
							end
						end
					end)
					
					
				
				end
			end
		end
		end;
}

return DamageHandler

Since you want to run this once you can do the following instead:

EnemyCharacter.Humanoid.Died:Wait() --waits until the enemy dies
--the code you had inside the Died event goes here

That will only run once no matter what unless there’re multiple coroutines/scripts executing this piece of code. Also, you may want to wrap it inside a coroutine if you don’t want to yield the script(for example if this code is part of a loop for all the enemies):

task.spawn(function()
	EnemyCharacter.Humanoid.Died:Wait()
	--the code you had inside the Died event
end)
1 Like

humanoid.Died:Connect()

This connects a function to the died event. Right now you are connecting a function to that event every time somone takes damage

1 Like

It seems its not working

blur

Because it connects the died event every time damage is taken. there will be numerous events firing when the character dies

1 Like

thank you for explaing it easy

It can also be connected to a :Once statement

i.e. EnemyCharacter.Humanoid.Died:Once(FUNCTIONNAMEGOESHERE)

1 Like

I havent ever heard about it wow

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