Unit kill counter? | Roblox tower defense

How can I make a kill counter for a unit that stores the value for each kill upon upgrade(because a new version of the unit is spawned when upgraded). so I cant store the kill values inside the unit because it gets replaced by a new one

here is the attack function

function UnitSpawnModule.Attack(newUnit, player)
	local Config = newUnit:WaitForChild("Config")
	local target = UnitSpawnModule.FindTarget(newUnit, Config.Range.Value, Config.TargetMode.Value)
	
	
	if target and  target:FindFirstChild("Humanoid") and target.Humanoid.Health > 0 then
		if newUnit.Config.AttackType.Value == "Slasher" then
			local targetcframe = CFrame.lookAt(newUnit.HumanoidRootPart.Position, target.HumanoidRootPart.Position)
			newUnit.HumanoidRootPart.BodyGyro.CFrame = targetcframe
			print(newUnit.Name.." Is a Slasher")
			
			if newUnit.Animations.Fire:FindFirstChild("Amount") then
				AnimateMultiTower:FireAllClients(newUnit, "Fire", target)
			end
			

			target.Humanoid:TakeDamage(Config.Damage.Value)
			newUnit.Head.Sword:Play()
			
			
			
			
			local killsValue = 0
			
			
            if target.Humanoid.Health <= 0 then
				killsValue += 1
				
				player.Money.Value += target.Money.Value
				player.Kills.Value +=1
				newUnit.Config.Kills.Value = killsValue
				
				print(killsValue)
				
				
			
			end
			task.wait(Config.Cooldown.Value)
			
			
			
		end
			

	end
	task.wait(.1)
	if newUnit and newUnit.Parent then
		UnitSpawnModule.Attack(newUnit, player)
	end
	
end

and here is the kill counter that I tried to make:

local killsValue = 0
			
			
            if target.Humanoid.Health <= 0 then
				killsValue += 1
				
				player.Money.Value += target.Money.Value
				player.Kills.Value +=1
				newUnit.Config.Kills.Value = killsValue
				
				print(killsValue)
				
				
			
			end
			task.wait(Config.Cooldown.Value)
			

but the problem with this is that the kill counter resets back to zero when the function is called… How can I make it so that the kill counter is stored and is unique to that specific type of unit ? thank youu

2 Likes

My friend, what this function is doing is allocating a new object in memory with the id ‘killsValue’, type number and value 0.

then you stating to the program to increase the value stored within the object, and once the function ends :

You lose the only variable that points to that object (address memory) where your data is stored and so the object is scheduled to be garbage collected. Because you have no reference to it anymore, you are unable to access it and modify it in any shape or form because Luau doesn’t give you a way to interact directly with that memory address.

What you want to do is to not lose the variable you have defined, so it can never be garbage collected.

You have to cache the variable.

local kills_cache = {}

local function increment_kills(player)
      if not kills_cache[player] then
           -- allocate new storage in memory to store {kills = 0}
           kills_cache[player] = {kills = 0}
           return
      end
     kills_cache[player].kills += 1
end

for _, player in ipairs(game.Players:GetPlayers()) do
   increment_kills(player)
end