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?
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
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.
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
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.
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