Get money after killing a NPC

Hello, Ive been trying to make it so that after killing an NPC, the player earns money.
I have followed multiple tutorials and guides, but still dont unterstand how to script it… I need help on how to make the “creator tag” work.

These are the scripts I currently have;

In the NPC:

local Humanoid = script.Parent.Humanoid
function Dead()
	local tag = Humanoid:FindFirstChild("creator")
	if tag ~= nil then
		local leaderstats = tag.Value:FindFirstChild("leaderstats")
		if leaderstats ~= nil then
			leaderstats.Cash.Value = leaderstats.Cash.Value +10
			wait(0,1)
			script:Remove()
		end
	end
end
Humanoid.Died:Connect(Dead)

In the sword:

local tool = script.Parent
local canDamage = false
local canSwing = true

local function onTouch(partOther)
	
	local humanOther = partOther.Parent:FindFirstChild("Humanoid")
	
	if not humanOther then return end
	
	if humanOther.Parent == tool then return end
	
	if humanOther.Parent ~= tool.Parent and canDamage then 
		humanOther:TakeDamage(50)
	else
		return
	end
	
	canDamage = false
end

local function slash()
	if canSwing then
		canSwing = false
		local str = Instance.new("StringValue")
		str.Name = "toolanim"
		str.Value = "Slash" 
		str.Parent = tool
		canDamage = true
		wait(0.5)
		canSwing = true
	end
end

tool.Activated:Connect(slash)
tool.Handle.Touched:Connect(onTouch)

Assuming the sword script is a server script, you could do something as simple as adding to your onTouch function:

local player = tool.Parent.Parent // or another way of getting the player the sword belongs to

local function onTouch(partOther)
	
	local humanOther = partOther.Parent:FindFirstChild("Humanoid")
	
	if not humanOther then return end
	
	if humanOther.Parent == tool then return end
	
        if humanOther.Parent ~= tool.Parent and canDamage then 
		humanOther:TakeDamage(50)
        if humanOther.Health <= 0 then // if that blow killed the npc             
          player:FindFirstChild(“leaderstats”).Cash.Value += 10 — or whatever amount
        end
	else
		return
	end
	
	canDamage = false
end

sorry about the poor formatting i’m on my phone, it also doesn’t have the proper character to make comments

1 Like

Thank you so much! Ive got it to work!

2 Likes