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
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
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:
The target might not have a child named “Humanoid”. If target:FindFirstChild("Humanoid") returns nil, it means there’s no child with that name.
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”.
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
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