Reward When Killing NPC

  1. 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)
  2. 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
  3. 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

Any help appreciate !

Hey Did u store zombie npcs in a folder or all of them on the workspace?

I store all zombie in a folder

can u give the full script pls?

I written the full script on original post :slight_smile: if you want I can give you the script that was in each npc

No there isn’t, you didn’t add local Collec = game:GetService”CollectionService”

oh oops sorry, I totally forgot to add this line (I added it to the original post)

1 Like

how do u reward the player with points when npc dies, like do u got the system for it

when the npc die, the script search for the player instance (tag) in npc humanoid, and add to the stats the value that I written

Meanwhile I’m trying some “solution” hoping that work, but still nothing at the moment

Well nvm seem like I need to do a reward script for each npc ,but still thank you for your help guys

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)
3 Likes

Thank you so much!!! It work perfectly I’m so happy :smiley: ! I was blocked cause of that!! Really tysm!!

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