How to make an give you cash on kill?

So I’m making a zombie game and my idea was that when you kill the zombie he gives you cash. So how would I be able to do that? (my leaderstat is called “cash”)

9 Likes

A quick google search could have given you your anwser, look at these tutorials that could possibly help you out!!

Hope these helped! :smile:

9 Likes

Thanks mate, I’ll try them but the reason why I asked this on the forum is because I have followed alot of tutorials about this but non of them worked. :confused:

4 Likes

These videos didn’t work either as they’re for players only. :confused:

3 Likes

Hello there! May I know what system you’re using?

Are you using tools? What kind - melee or ranged? Can you provide any relevant scripts?


I can only give you the gist of how a system might work:

  1. Tag the target with any id of the player (like name, userid, etc) If there is already a tag on the target, you can either just add another tag or override the previous tag depending on whether you just want the person who killed it.
  2. Give the tag a clear time - after x seconds, the tag is removed. If there was a pre-existing tag with the same player, update the clear time of the tag or replace it with a new tag with the updated clear time.
  3. If the target is killed with any tags, reward the player(s) associated with any tags on the target.

(this uses a tag-based system - there are other methods but this is a nice basic and universal one.)


Do search the DevForum closely, I have found multiple topics that do go into this sort of system, even if it’s hidden in replies.

4 Likes

What do you mean they are for players only? Are you trying to attempt this with NPCs?

2 Likes

I believe he wants it so when you kill an npc, it awards you cash.

Your tutorials you posted all showed the script relying on the targets being Player objects, which they just simply aren’t. It is indeed an easy translation from Player to NPC, but perhaps not at OP’s skill level quite yet.

4 Likes

There’s more than one way to do this, but none of them are extremely simple. The best approach to take here would be to realize the ways you can tell the script that the zombie has been killed by a player. You need three bits of information for this: the player, the zombie’s health, and whether or not the zombie has already died.

You need to know the player for self-explanatory reasons; you can’t reward cash if the player being rewarded isn’t known. Similarly, checking the zombie’s health is pretty self-explanatory, as you need to make sure the health is at 0. You could connect the humanoid of the zombie to a .Died event and then check for a tag and this certainly isn’t an uncommon practice, but that would likely require more scripting and is generally paired with an overall leaderboard script which you’d have to write separately, so I’ll cover the shorter method which will only require you to edit the weapons themselves. Lastly, you want to check for whether or not the zombie has already died because if the health reads 0 after the zombie takes damage, it doesn’t mean the player killed the zombie. It’s possible they simply attacked the zombie while it was already dead.

So how do you do all of that? You’ll need to edit the portion of the weapons you’re using which damage the zombie. I’ll write an example script segment, then walk you through it to explain it.

--assume this script is in the tool 

local tool = script.Parent 
local player = nil 

tool.Equipped:Connect(function() 
	
	local plr = game.Players:GetPlayerFromCharacter(tool.Parent) 
	
	if plr then 
		
		player = plr --get player when tool is equipped 
		
	end 
	
end) 

--later in the script (where the zombie is being damaged) 

local human = zombie:FindFirstChildWhichIsA("Humanoid") 

if human and human.Health > 0 then --check if dead yet 
	
	human.Health = human.Health - 5 --deal damage 
	
	if plr and human.Health <= 0 then --check if killed and if player exists 
		
		plr.leaderstats.Cash.Value = plr.leaderstats.Cash.Value + 5 
		
	end 
	
end 

If you have any weapons which have a notable delay between when they’re used and when they deal damage, it’s better to use a tag, but previous posts on this thread have already covered the use of tags.

4 Likes

So I just have to put that script in the weapon?

1 Like

Well then, I’ve found more, these could or could not help!

4 Likes

Unfortunately no, there isn’t really a simple script you can just copy-paste into your weapon for it to magically work. No matter what the solution is, you’ll have to intertwine it with the existing code for the weapon. If you learn a few basics, it shouldn’t be a problem just editing some existing code.

3 Likes

I would like to use guns but the zombie npc’s don’t take damage when I shoot them.

2 Likes

That’s easy to fix! Use Raycasting to detect which target is hit. Test if the target is a zombie, then damage it. Make sure you’re finding the Humanoid of the zombie by class so that way if the humanoid is named something else, it can still damage it. You can check if its dead after damaging it and reward the player who fired the ray.

Unfortunately, the best tutorial of a raycasting gun I have is this tutorial which has seen some criticism recently for following some bad practices, but you can still work out the basics.

4 Likes

you could make a folder in the zombies and store objectvalues into the zombie, then add a script in the zombies when he dies he’ll get all values in the folder and add cash into that objectvalue values

2 Likes

Maybe we could make them drop cash so you have to hit it. I think that we just need to code the money that when you hit it it gives you money.

2 Likes

I have followed the tutorial and it works but the zombie doesn’t take damage. :confused:

2 Likes

Can I see your script and setup?

That tutorial may be a bit outdated.

2 Likes

Sure, do you have discord? that’s maybe easier to do that cause I could stream it out to you then.

2 Likes

It would be more beneficial for you to post it here via copy & paste.

I’m more interested in the code and location of the script, emphasis on the code. Do paste it here so that other people can help diagnose problems in it.

3 Likes

Btw, what do you mean by setup?

2 Likes