KillForCash not working

So I want it to be where when I kill an npc, my gold goes up. It doesn’t work

My Script:

local humanoid = script.Parent.humanoid

function killforgold()
	print("killed")
	local tag = humanoid:FindFirstChild("creator")
	if tag ~= nil then
		print("success 1")
		if tag.Value ~= nil then
			print("success 2")
			local leaderstats = tag.Value:FindFirstChild("leaderstats")
			if leaderstats ~= nil then
				print("success 3")
				leaderstats.Gold.Value = leaderstats.Gold.Value +100
			end
		end
	end
end

humanoid.Died:Connect(killforgold)

I put in prints to help me debug the problem
It doesn’t go past the “killed” print

I can’t figure out the problem, help?

2 Likes

Likely the tag isnt found.

print(tag) to make sure of this.

Well then, look at what’s after it.

You looking to see if the tag exists. At the time of checking it, it does not exist. The creator tag is likely inserted shortly after death.

I recommend doing damage after inserting the creator tag into the humanoid.

1 Like

Changed print(“success 2”) to print(tag)

The output only showed “killed”

Thanks so much for the fix! Sorry if I seemed dumb, I’m a beginner scripter.

It’s okay! I’ve been programming for 7 years and I still make mistakes like this all the time, it’s super common!

1 Like