NPC not giving cash after first death

After the NPC is killed by the npc, the player receives it cash, exp and all that… BUT, after the NPC dies and does the same thing, it does not gives the player cash.

The script gives the player cash.

--Services--
local work = game:GetService("Workspace")

--Variables--
local ZombieKilled = game:GetService("ReplicatedStorage").Remotes:WaitForChild("zombieKilled")

local ZombiesFol = work.Map:WaitForChild("Zombies")

--Script

for _, zombie in pairs(ZombiesFol:GetChildren()) do
	
	local zombiehum = zombie:FindFirstChild("Zombie")
	
	if zombiehum then
		
		zombiehum.Died:Connect(function()
			
			local tag = zombiehum:findFirstChild("creator")
			if tag ~= nil then
				if tag.Value ~= nil then
					local Leaderstats = tag.Value:findFirstChild("leaderstats")
					if Leaderstats ~= nil then
						Leaderstats.Cash.Value = Leaderstats.Cash.Value + math.random(9, 13)
						Leaderstats.Kills.Value = Leaderstats.Kills.Value + 1
						Leaderstats.Exp.Value = Leaderstats.Exp.Value + math.random(24, 29)
			
					end
				end
			end
			
		end)
		
	end
	
end

The NPC humanoid is named “Zombie”. Any ideas or reasons why it would be working like that?

What does this mean? How can an NPC kill something once it’s already dead? What are you having a problem with?

Also, :findFirstChild() is deprecated, use :FindFirstChild()

The NPC is respawned once it dies. The problem that I am having is the NPC not giving the player cash after killing it after the NPC died a first time

It’s because you iterated through the Zombies folder a single time, meaning that a .Died function was only linked to the zombies in the folder at the time of your loop.

Each time you spawn a new NPC, you need to link a new .Died function to it for it to work

Alright, how would I do this? would I have to use while wait(number) do or something like that?

How are you respawning each zombie? In the script where you respawn zombies, each time you spawn one in, link a .Died function to it

local humanoid = script.Parent:WaitForChild("Zombie")

humanoid.BreakJointsOnDeath = false

z = script.Parent

backup = z:clone()

z.Zombie.Died:Connect(function()	
	for index,joint in pairs(script.Parent:GetDescendants()) do
		if joint:IsA("Motor6D") then
			local socket = Instance.new("BallSocketConstraint")
			local a1 = Instance.new("Attachment")
			local a2 = Instance.new("Attachment")
			a1.Parent = joint.Part0
			a2.Parent = joint.Part1
			socket.Parent = joint.Parent
			socket.Attachment0 = a1
			socket.Attachment1 = a2
			a1.CFrame = joint.C0
			a2.CFrame = joint.C1
			socket.LimitsEnabled = true
			socket.TwistLimitsEnabled = true
			joint:Destroy()
		end
	end
	z.LowerTorso["Damage Script"]:Destroy()
	wait(4)
	z:Remove()
	wait(2)
	backup.Parent = game.Workspace.Map.Zombies
	backup.Head:MakeJoints()
	backup.UpperTorso:MakeJoints()
	backup.LowerTorso:MakeJoints()
end)

Want me to explain how it works or?

Yes.

Also, you should always use task.wait() instead of wait(). wait() has been deprecated for a while, and it’s just worse, there’s no benefit to using it.

Alrighty, I will use task.wait(). I don’t mean to be asking, but where exactly would I put task.wait()?

Long story short, it’s an improved version of wait(). Just replace every wait() with task.wait().

If ROBLOX could remove wait(), they would, but since so many people use wait(), it would break a ton of games so they don’t remove it.

If you want the long story, look up the roblox Task Scheduler, or see this thread:

Also, you still haven’t explained your script

Yes, I have been through that devofrum, but I meant, where would I put task.wait() in my script? Before the script runs after making its variables?

literally just replace all the wait() you have in your scripts with task.wait()

I don’t have any wait() in the script that I am using to reward the players for ykyk.

Every time you respawn a zombie, you need to link a new .Died function to it.

In your for loop in the first script you showed, you created a .Died function for each zombie, correct? For each time you spawn a new zombie, you just need to make a new .Died function for that zombie. There’s no extra steps

I probably should’ve mentioned this but this script is a script and is located in the ServerScriptService so I am not quite sure how I would make a new .Died function in ServerScriptService

You had this in your script:

zombiehum.Died:Connect(function()
			
			local tag = zombiehum:findFirstChild("creator")
			if tag ~= nil then
				if tag.Value ~= nil then
					local Leaderstats = tag.Value:findFirstChild("leaderstats")
					if Leaderstats ~= nil then
						Leaderstats.Cash.Value = Leaderstats.Cash.Value + math.random(9, 13)
						Leaderstats.Kills.Value = Leaderstats.Kills.Value + 1
						Leaderstats.Exp.Value = Leaderstats.Exp.Value + math.random(24, 29)
			
					end
				end
			end
			
		end)

This is a .Died function. In your for loop you have in your script right now, you create a .Died function that is triggered each time the zombie’s humanoid’s health reaches zero. All you need to do is literally link a function exactly like this to any new zombie you create.

Just do this:

Put inside NPC:

Hum.Died:Connect(function() -- Your Humanoid
	local tag = Hum:FindFirstChild("creator")
	if tag ~= nil then
		if tag.Value ~= nil then
			local ls = tag.Value:FindFirstChild("leaderstats")
			if ls ~= nil then
				ls.Cash.Value += math.random(9, 13)
				lss.Kills.Value += 1
				wait(0.1)
				script:Remove()
			end
		end
	end
end)

Alright, so to link a function to any new zombie that is created by their respawn script, I would need to use “while task.wait() do” before the line that finds all the NPC in the NPC folder, right?

No. Literally each time you respawn a zombie, link a .Died function to it. Go to the code where you respawn zombies, and create a .Died function each time a zombie is respawned