What do you want to achieve? Keep it simple and clear!
I want to give points and kill to player stats when a npc die (With Multiple type of npc) in only 1 script (to avoid lags and datastore issue), and that the npc give the stats if he get killed multiple time (not only once)
What is the issue? Include screenshots / videos if possible!
Using my current script, I get stats only for the npc that I didnt killed multiple time,those that I eliminate again doesnt give stats
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
moving from using GetChildren()) to CollectionService but still the same issue
This is the current code that I use to give stats to player
local Collec = game:GetService("CollectionService")
for i,zombie in Collec:GetTagged("Zombies") do
zombie:FindFirstChild("Humanoid").Died:Connect(function()
local Tag = zombie:FindFirstChild("Humanoid"):FindFirstChild("Killertag")
if Tag then
if Tag.Value then
local Leader = Tag.Value:FindFirstChild("leaderstats")
if Leader then
if zombie.Name == "BigZombie" then
Leader.Kills.Value = Leader.Kills.Value + 1
Leader.Points.Value = Leader.Points.Value + 30
elseif zombie.Name == "SpeedZombie" then
Leader.Kills.Value = Leader.Kills.Value + 1
Leader.Points.Value = Leader.Points.Value + 20
elseif zombie.Name == "SlowZombie" then
Leader.Kills.Value = Leader.Kills.Value + 1
Leader.Points.Value = Leader.Points.Value + 25
elseif zombie.Name == "Zombie" then
Leader.Kills.Value = Leader.Kills.Value + 1
Leader.Points.Value = Leader.Points.Value + 15
end
end
end
end
end)
end
You’ve to also use CollectionService:GetInstanceAddedSignal(), what you’re doing currently is running this script for only currently existing zombies, you also have to run it for newly added zombies so it’d become:
-- Make sure the names are correct, first value is the amount of kills
-- second value is the amount of points.
local zombieStats = {
["BigZombie"] = {[1] = 1, [2] = 30};
["FastZombie"] = {[1] = 1, [2] = 30};
["SlowZombie"] = {[1] = 1, [2] = 30};
["Zombie"] = {[1] = 1, [2] = 30};
}
local function giveStats(leaderstats, zombieName)
if not leaderstats then return end
local stats = zombieStats[zombieName]
if stats then
leaderstats.Kills.Value += stats[1]
leaderstats.Points.Value += stats[2]
else
warn(zombieName .. " is not part of the zombie stats array!")
end
end
local function setupTaggedZombie(zombie)
local humanoid = zombie and zombie:FindFirstChildWhichIsA("Humanoid")
if not humanoid then return end
humanoid.Died:Connect(function()
local killerTag = humanoid and humanoid:FindFirstChild("Killertag")
if not killerTag.Value then killerTag:GetPropertyChangedSignal("Value"):Wait() end
if killerTag then
giveStats(killerTag:FindFirstChild("leaderstats"), zombie.Name)
end
end)
end
-- Already existing zombies
for _, zombie in Collec:GetTagged("Zombies" do
setupTaggedZombie(zombie)
end
-- Newly added zombies
Collec:GetInstanceAddedSignal("Zombies"):Connect(setupTaggedZombie)