Roblox kill leaderboard for projectiles

How do I make it so that kills from a projectile count as kills towards a leaderboard?

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new ("Folder",player)
	leaderstats.Name = "leaderstats"

	local kills = Instance.new("NumberValue",leaderstats)
	kills.Name = "Kills"
	kills.Value = 0

	local deaths = Instance.new("NumberValue",leaderstats)
	deaths.Name = "Deaths"
	deaths.Value = 0

	player.CharacterAdded:Connect(function(character)
		local humanoid = character:FindFirstChild("Humanoid")

		humanoid.Died:Connect(function(died)
			deaths.Value = deaths.Value + 1
			local tag = humanoid:FindFirstChild("creator")
			local killer = tag.Value
			if tag and killer	then
				killer.leaderstats:FindFirstChild("Kills").Value = killer.leaderstats:FindFirstChild("Kills").Value + 1
			end
		end)
	end)

end)

The script works when the player uses a sword, but not when the player uses a ranged weapon like a gun. Does anyone know how I can fix this?

2 Likes

Is the tag value being created/changing when the player is hit?

2 Likes

it is being chanegd when you kill someone. but the value wont go up if your technically not “touching” the player. Even though the game is based around ranged weapons.

2 Likes

Can I see the code where you change it?

2 Likes
game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new ("Folder",player)
	leaderstats.Name = "leaderstats"

	local kills = Instance.new("NumberValue",leaderstats)
	kills.Name = "Kills"
	kills.Value = 0

	local deaths = Instance.new("NumberValue",leaderstats)
	deaths.Name = "Deaths"
	deaths.Value = 0

	player.CharacterAdded:Connect(function(character)
		local humanoid = character:FindFirstChild("Humanoid")

		humanoid.Died:Connect(function(died)
			deaths.Value = deaths.Value + 1
			local tag = humanoid:FindFirstChild("creator")
			local killer = tag.Value
			if tag and killer	then
				killer.leaderstats:FindFirstChild("Kills").Value = killer.leaderstats:FindFirstChild("Kills").Value + 1
			end
		end)
	end)

end)

This is the code. It just wont work because the kill is being used with a weapon that isnt touching both players at the same time.

Melee weapons work because both players are being connected in the kill
ranged weapons wont work because the player is far away from the other player they are killing.

I have a similar error to this post if what I am saying is confusing.

2 Likes

When trying to add Kills to leaderstats with a Projectile, you use this code:

local Debris = game:GetService("Debris")
local function TagHumanoid(humanoid, player) 
--[[ Creates a Variable called "Creator",
 this is what the ROBLOX Classic Sword Uses to Change the player's points
]]
	local Creator_Tag = Instance.new("ObjectValue")
	Creator_Tag.Name = "creator" -- The Name
	Creator_Tag.Value = player -- Value is equal to player, from there you can edit the Players Items
	GS.Debris:AddItem(Creator_Tag, 2) -- Removes Item without disrupting the code
	Creator_Tag.Parent = humanoid -- gives the Variable to Humanoid
end

local function UntagHumanoid(humanoid)
	for i, v in pairs(humanoid:GetChildren()) do
		if v:IsA("ObjectValue") and v.Name == "creator" then
			v:Destroy()
		end
	end
end

And then Inside a RemoteEvent, do this:

-- Before this determine if the RayCast or the Projectile has hit a Humanoid, otherwise this will error
TagHumanoid(Humanoid, Player)

This is where your code comes in:

humanoid.Died:Connect(function(died)
			deaths.Value = deaths.Value + 1
			local tag = humanoid:FindFirstChild("creator")
			local killer = tag.Value
			if tag and killer	then
				killer.leaderstats:FindFirstChild("Kills").Value = killer.leaderstats:FindFirstChild("Kills").Value + 1
			end
		end)
	end)
3 Likes

im curious how I would implement this into my leaderstats script. Do I get rid of the old “kill adder” script and replace it with that. And then add in a remot event and add a localscript into the remote event with the bottom script?

1 Like

You dont, you put it inside the Tool or Item you are Using.


Really Depends, up to you tho


Use OnServerEvent() (Server Script) for the RemoteEvent, then Activate it with the LocalScript

Example of One of my Weapons Using the Code:

Tool = script.Parent

GS = require(script.Parent.WeaponSetup)

local function TagHumanoid(humanoid, player)
	local Creator_Tag = Instance.new("ObjectValue")
	Creator_Tag.Name = "creator"
	Creator_Tag.Value = player
	GS.Debris:AddItem(Creator_Tag, 2)
	Creator_Tag.Parent = humanoid
end

local function UntagHumanoid(humanoid)
	for i, v in pairs(humanoid:GetChildren()) do
		if v:IsA("ObjectValue") and v.Name == "creator" then
			v:Destroy()
		end
	end
end

script.Parent.Fire.OnServerEvent:Connect(function(plr,mousepos)

	local RCP = RaycastParams.new()
	RCP.FilterDescendantsInstances = {plr.Character}
	RCP.FilterType = Enum.RaycastFilterType.Blacklist

	local RCR = workspace:Raycast(Tool.Handle.Position, (mousepos - script.Parent.Handle.Position) * 300,RCP)


	if RCR then
		local HP = RCR.Instance
		local model = HP:FindFirstAncestorOfClass("Model")


		if model then
			for _,Humanoid in pairs(model:GetChildren()) do
				if Humanoid.Name == GS.DesiredHumanoid then
					pcall(function() -- To Prevent Error
						TagHumanoid(Humanoid, plr)
						Humanoid:TakeDamage(GS.Damage)
					end)
				end
			end

		end	

	end
	GS:PrimaryFireEvent()
end)

2 Likes

I am still confused. Thank you for the help. I am going to go to bed and see if I can figure it out tomorrow.

2 Likes

This is just a moduleScript i uded to store certain Data, you dont have to use this tho.

2 Likes
function OnRayHit(cast, raycastResult, segmentVelocity, cosmeticBulletObject)
	-- This function will be connected to the Caster's "RayHit" event.
	local debreeService = game:GetService("Debris")
	local function tagHumanoid(humanoid, player)
		local creator_tag = Instance.new("ObjectValue", humanoid)
		creator_tag.Name = "creator"
		creator_tag.Value = player
		Debris:AddItem(creator_tag, 9)
	end
	
	local function untagHumanoid(humanoid)
		for i, v in pairs(humanoid:GetChildren()) do
			if v:IsA("ObjectValue") and v.Name == "creator" then
				v:Destroy()
			end
		end
	end
	
	
	local hitPart = raycastResult.Instance
	local hitPoint = raycastResult.Position
	local normal = raycastResult.Normal
	if hitPart ~= nil and hitPart.Parent ~= nil then -- Test if we hit something
		local function onDamage(player, target, damage)
			
			local humanoid = hitPart.Parent:FindFirstChildOfClass("Humanoid") -- Is there a humanoid?
			untagHumanoid(humanoid)
			tagHumanoid(humanoid, player)
		if humanoid then
			humanoid:TakeDamage(100) -- Damage.
		end
			MakeParticleFX(hitPoint, normal) -- Particle FX
		end
	end
end

hey so this is what I have right now. I feel like it should be working but, the projectiles dont deal damage. They did before I added all of that creator tag stuff. do you know the issue?

2 Likes

Put the functions outside, not inside

2 Likes

I put the function on the outside but now the kills leaderboard value doesn’t go up. here is my other script.

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local humanoid = character:FindFirstChild("Humanoid")
		
		humanoid.Died:Connect(function()
			local tag = humanoid:FindFirstChild("creator")
			if tag then
				local player = tag.Value
				local kills = player.leaderstats.Kills
				kills.Value = kills.Value + 1 
			end
		end)
		end)
	end)
2 Likes