Leaderstats on kill

Hi i wanted to know ho wi can make it so its adds points to your leaderstats when you kill a player

3 Likes

You need a tagging feature in your weapons, and when a player dies you add a point to their killers death value.

how can i do this im very new to scripting

why would you need a tagging system? I find that a lot of weapon systems have tagging systems but why can’t the script that initially caused whatever bullet or sword to cause damage to the player be the one to register the kill?

I learned from roblox documents and youtube. I would recommend to start here: (27) How to Make a Roblox First Person Shooter 3 (Kills Leaderboard) - Roblox Studio Tutorial - YouTube

I mean you don’t need a tagging system but it’s one way of doing this. Another way to do this would be to calculate if enemyHealth - damageDealt is under 0 and if it is, then increase the value for the player that used the tool. There’s multiple ways to do everything, and it’s just preference. My preference is the tagging system because if someone gets hurt and then resets to regain health, the person who dealt that damage would get their kill. Although you should clear the tag after a little bit, in case someone resets and the last person who dealt them damage did so like 5 minutes ago.

After reading my post and implementing the code accordingly you should receive this result: https://youtu.be/U4h7S7Q1feQ
(The entities lag like this when dying for me in Studio, this is not caused by my script.)
I did only use the mesh of the “ClassicalSword” from Roblox. The code was written by myself, but as far as I’m concerned this should also work without having to change anything from the “ClassicalSword” free model.

So a method I recommend is tagging the entity affected negatively by your weapon.
This method is also included in the ClassicSword from Roblox and you mainly need 2 simple functions.

Tagging the Humanoid:

local function tagHum(hum, plr)
	local tag = Instance.new("ObjectValue")
	tag.Name = "creator"
	tag.Parent = hum
	tag.Value = plr
end

Untagging the Humanoid:

local function untagHum(hum)
	local tag = hum:FindFirstChild("creator")
	if tag then tag:Destroy() end
end

You will apply these functions exactly before damaging the Humanoid. Let’s use a very simple weapon script.

local tool = script.Parent

local damage = 20

local function onHit(hit)
    -- first, get the player who is using the weapon
	local plr = game:GetService("Players"):GetPlayerFromCharacter(tool.Parent) 
	if plr == nil then return end

    -- get the humanoid of the entity hit
	local hum = hit.Parent:FindFirstChild("Humanoid")
	if hum and hum.Health > 0 then
		untagHum(hum) -- untag the humanoid
		tagHum(hum, plr) -- tag the humanoid
		hum:TakeDamage(damage) -- damage the humanoid
	end
end

tool.Handle.Touched:Connect(onHit) -- connect function to event

Why do I have to untag the humanoid?
If you don’t, there might be a chance that a player who has caused damage to the same entity before, gets the kill stat and not the player who actually killed the entity.

Now that was not everything. You will also have to give the entity a server-side script to handle this tag. Of course, you don’t give every NPC the script manually, you can automate this process using a gamehandler script.

Before saying this you will need to customise this script to your path of leaderstats.
I will assume that you have a leaderstats folder inside every player and an IntValue called “Kills”.

I would recommend to call this script “deathScript”.

local rig = script.Parent

local function updateStats()
	local hum = rig:FindFirstChildOfClass("Humanoid")
	if not hum then return end
	local tag = hum:FindFirstChild("creator")
	if tag then
		if tag.Value then
			local leaderstats = tag.Value:FindFirstChild("leaderstats")
			if leaderstats then
				local Kills = leaderstats:FindFirstChild("Kills")
				Kills.Value = Kills.Value + 1
			end
		end
	end
end

rig.Humanoid.Died:Connect(updateStats)

If something doesn’t work, reply to this and I’ll help you as soon as I can.

1 Like

probably should have specified i want it to add a value called coins to the killer or the person that died

Come on man. It’s just a few variables you could have changed in code. But okay.

This should work for you then:

local rig = script.Parent

local function updateStats()
	local hum = rig:FindFirstChildOfClass("Humanoid")
	if not hum then return end
	local tag = hum:FindFirstChild("creator")
	if tag then
		if tag.Value then
			local leaderstats = tag.Value:FindFirstChild("leaderstats")
			if leaderstats then
				local Coins = leaderstats:FindFirstChild("Coins")
				Coins.Value = Coins.Value + 100
			end
		end
	end
end

rig.Humanoid.Died:Connect(updateStats)

As the upper script is probably more helpful to others please mark that one as solution. :slight_smile:

1 Like

im a modler mainly i only started scripting 2 days ago

Oh that’s nice. Keep going man!

by the way. the “+100” in the code above is the amount of coins the player receives. you can change that to whatever you like.

ok thanks i know its not good but i have this so far

(1) Weapon Simulator - Roblox

Come on man. It’s just a few variables you could have changed in code. But okay.

Talking about this, I’ve noticed you (as in the person who created this topic) have created a lot of topics recently, which mostly ask basic questions which you could have found the answer to if you had looked it up. Please make sure to look up your question first before making a topic.

i know i dont want to just copy and paste them i want to see what other people do to understand how it works

No, I did not create this topic. I created one topic as I’m speaking and it’s not solved yet.

I meant the person who created this topic, aka @Diam0ndzOnYT, not you

Oh, the quote confused me sorry.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.