Help with Npc giving Cash when killed

Hello!

I am currently trying to figure out how to make Npcs give cash when the Player kills them! I don’t want then to drop the Cash but I want the leaderstats Cash to go up by a certain number.

I have tried to watch some tutorials but the problem is that whenever I play test the script, the Cash wouldn’t add up to 10 or 5! There were no errors in the output.

Here is the script from the tutorial:

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

I am using the official Roblox Drooling Zombie (Rthro) Npc. I feel like its the model itself but I’m not sure, how can I make this possible?

Don’t describe the problem you’re having without providing the script you used.

Sorry! I forgot to put the script in, I was in a rush!

What weapons do you use?
You need to make sure your weapon creates a creator tag every hit.

The weapons I use are the Official Roblox Weapons, mostly the guns, like the auto gun or the pistol

Lets do some debugging. Can you check the output after using the following code?

local Humanoid = script.Parent.Humanoid

local function onDied()
	local tag = Humanoid:FindFirstChild("creator")
	if tag and tag.Value then
		local leaderstats = tag.Value:FindFirstChild("leaderstats")
		if leaderstats then
			leaderstats.Cash.Value += 15
			script:Destroy()
		else
			error("Player is missing leaderstats!")
		end
	else
		print("No tag was found or the tag's value was not set.")
	end
end

Humanoid.Died:Connect(onDied)
1 Like

So the output has printed “No tag was found or the tag’s value was not set.”

This suggests that the guns you are using are not creating the ‘creator’ tag you’re expecting to see.

Put this Script (not a LocalScript) in a Tool and make sure the Tool has ‘HandleRequired’ disabled

Please Note - I haven’t had the chance to test this gun - it is very simple but should provide the tag and enemy damage when the projectile collides

local tool = script.Parent

tool.Activated:Connect(function()
	local char = tool.Parent
	local player = game.Players:GetPlayerFromCharacter(char)
	local hrp = char.HumanoidRootPart

	local projectile = Instance.new("Part",workspace)
	projectile.Name = "Projectile"
	projectile.Size = Vector3.new(1, 1, 1)
	projectile.Shape = Enum.PartType.Ball
	Instance.new("BodyForce",projectile).Force = Vector3.new(0,projectile:GetMass()*workspace.Gravity,0) -- anti-gravity
	projectile.CFrame = hrp.CFrame * CFrame.new(0, -0.5, -1.5) -- set the offset of the project from the player's character - 0.5 studs up, 1.5 studs forward (if I've done my CFrames right that is :D)
	projectile.AssemblyLinearVelocity = project.CFrame.LookVector * 50 -- give the project a speed of 50 studs per second in the direction the project is pointing (which is relative to hrp.CFrame.LookVector)


	projectile.Touched:Connect(function(hit) -- when the projectile collides with something
		local hum = hit.Parent:FindFirstChildOfClass("Humanoid")
		if (hum) then
			local tag = Instance.new("ObjectValue",hum) -- 'creator' tag
			tag.Name = "creator"
			tag.Value = player
			hum:TakeDamage(25)
			game.Debris:AddItem(tag,0.5) -- destroy the tag automatically after half a second to reduce spam
		end
		projectile:Destroy()
	end)
end)

3 Likes