How to i add a kills leaderstat

Im trying to make a leaderstat that adds one value for every kill the player gets.

ive tried to use toolbox scripts and some youtube tutorials but none of them have worked.

i also dont want the kills to save.

3 Likes

Do you already have a weapon and a leaderstats script set up?

You could store the last player that hit you and gave you damage inside a variable, and once you die, you add a kill to their leaderstats (and maybe after some time, you can clear that variable so if you fall in the void and you were last hit 15 minutes ago, the kill doesn’t count towards that player). This should be done inside the server script. Never trust the client. Example:

You would put this inside the tool.

-- Inside the tool script
local players = game:GetService("Players")
local localPlayer = script.Parent.Parent.Parent

sword.Handle.Touched:Connect(function(part) -- Assume this is your function that deals damage to the player
    local plr = players:GetPlayerFromCharacter(part.Parent)
    if plr then
        local char = plr.Character
        local lastHit = char:FindFirstChild("LastHit") -- ObjectValue with the value being the player that lastly hit you
        if not lastHit then -- Creating the object value if it doesn't exist
            lastHit = Instance.new("ObjectValue")
            lastHit.Name = "LastHit"
            lastHit.Value = localPlayer
            lastHit.Parent = char
       else
            lastHit.Value = localPlayer
        end
    end
end)

And this inside StarterCharacterScripts

local character = script.Parent
local humanoid = character.Humanoid

humanoid.Died:Connect(function()
    local lastHitObj = character:FindFirstChild("LastHit")
    if lastHitObj then
         local plr = lastHitObj.Value
         if plr.Parent then
             plr.leaderstats.Kills.Value += 1
         end
    end
end)

NOTE: All scripts are server side scripts.

yea…,…,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,…,.,.,.,.,.,.,.,.,.,.,.,

I think this is a good suggestion, However I think if you already use tools you could also fire an event once the player using the tool has killed the player/npc. And there would be no need for client to server communication with this method I belive as opposed to storing the value which would have to be updated by the server anyway and stored by the server to maintain security.

1 Like

how would i be able to detect when a player is died and who killed the player

1 Like

So your tool does damage right? what you could do is have an additional if statement of

if (Humanoid.Health - damage) <=0 then
	--you killed them put your bindable event or other logic here
end

You don’t need any client to server communication for this to work.

it works very well but would this work with ranged weapons like a bomb or a sniper?

It would work the same way, considering your gun uses raycasts, you can check what player the ray hits and you use exactly the same code used in my example.

do bombs use it too.,…,.,…,.,.,…,…,.,.,…,.,…,

Thanks so much for the solution

Depends how the bombs are scripted, you could create a radius of the bomb and see what players are inside it. If there’s players in it, you change their last hit value.

I made a part to represent the radius and its still not working

Can we see the script? min 30char

local players = game:GetService("Players")
local localPlayer = script.Parent.Parent.Parent

script.Parent.Part.Touched:Connect(function(part) -- Assume this is your function that deals damage to the player
	local plr = players:GetPlayerFromCharacter(part.Parent)
	if plr then
		local char = plr.Character
		local lastHit = char:FindFirstChild("LastHit") -- ObjectValue with the value being the player that lastly hit you
		if not lastHit then -- Creating the object value if it doesn't exist
			lastHit = Instance.new("ObjectValue")
			lastHit.Name = "LastHit"
			lastHit.Value = localPlayer
			lastHit.Parent = char
		else
			lastHit.Value = localPlayer
		end
	end
end)

You could use workspace:GetPartsInPart(part) that returns a table with the touching parts, and you could look inside the table and check if there’s any character parts inside the table.

how would i be able to apply that to the current script

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