Help with Money Per Kill

so, my tower defense script detects if its a certain tower thats killing the target, and if it is, it gives you money. although, an error saying “attempt to index nil with ‘died’”, any help?

		if newTower.Name == "Mercenary" then
			target:FindFirstChild("Humanoid").Died:Connect(function()
				player.leaderstats.Money.Value += 1000 --an absurd amount of money for debugging
			end)
		end

where do you get the target argument from because it might not have a humanoid

It is a character.

charlimit

hmm well maybe u can try making a custom death detector by making a boolvalue called Dead in the zombie/enemy character and then a script

local candie = true

while wait() do
	if script.Parent.Humanoid.Health < 1 then
		if candie == true then
			candie = false
		script.Parent.Dead.Value = true
		script.Enabled = false
		print("died")
		end
	else

	end


end

then just instead do

	if newTower.Name == "Mercenary" then
			target:FindFirstChild("Humanoid").Dead.Changed(function()
				player.leaderstats.Money.Value += 1000 --an absurd amount of money for debugging
			end)
		end

The error message “attempt to index nil with ‘died’” suggests that at the time your script tries to connect to the Died event, it’s attempting to access a nil value.

Let’s break down the possible culprits:

  1. The target might not have a child named “Humanoid”. If target:FindFirstChild("Humanoid") returns nil, it means there’s no child with that name.
  2. The Humanoid might not have a ‘Died’ event. This is less likely because Roblox’s Humanoid objects typically do have a Died event.

This code ensures that there’s a child named “Humanoid” and this child is actually of type “Humanoid”. :point_down:t2:

if newTower.Name == "Mercenary" then
    local humanoid = target:FindFirstChild("Humanoid")
    
    if humanoid and humanoid:IsA("Humanoid") then
        humanoid.Died:Connect(function()
            player.leaderstats.Money.Value += 1000
        end)
    end
end

the death detector script errors out “attempt to index nil with “humanoid””

hmm strange with me it always worked fine, maybe the characters are being created late, do they even have a humanoid because in tds they dont always use those

thx! this method worked for me.

1 Like

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