Ranged weapons don't add to a kill counter

Hi there, I have a script that adds a value to leaderstats everytime someone kills a player. The issue is, is that ranged weapons don’t work with this and don’t add the value to the leaderstats (that being the amount of kills they have had). I have tried using a melee tool like a sword and that works perfectly, but ranged weapons do not. In this case, I am using a railgun.

Leaderstats script:

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

Railgun script where it checks if the humanoid has died:

if hit and hit~=nil then
				local h=hit.Parent:FindFirstChild("Humanoid")
				if h~=nil and h.Health>0 and GLib.IsTeammate(GLib.GetPlayerFromPart(script), GLib.GetPlayerFromPart(h))~=true then
					tagHumanoid(h)
					plr.leaderstats.Kills.Value = plr.leaderstats.Kills.Value + 1
					h:TakeDamage(damage)
				end

Hello again. Could you send the full script of rail gun script?

Alright, prepare because this is really big.

--Made by Stickmasterluke
--[[
	Fixed by ArceusInator 2/18/2015
	- Works with FE
	- No longer bumps unanchored blocks around
	- No longer moves anchored blocks
	- No longer teamkills
	- Now uses velocity to push bricks and it looks way cooler
--]]


sp=script.Parent
plr=game.Players.localPlayer

local GLib = require(206209239)

damage=100
firerate=1
range=1000
barreloffset=Vector3.new(0, 0.25, -1.7)
windvec=Vector3.new(2,-1,1).unit

rate=1/30
debris=game:GetService("Debris")
equipped=false
check=true

function tagHumanoid(humanoid)
	local creator_tag=Instance.new("ObjectValue")
	creator_tag.Value=game.Players.LocalPlayer
	creator_tag.Name="creator"
	debris:AddItem(creator,3)
	creator_tag.Parent=humanoid
end

function checkintangible(hit)
	if hit and hit~=nil then
		if hit:IsDescendantOf(sp.Parent) or hit.Transparency>.8 or hit.Name=="Handle" or hit.Name=="Effect" or hit.Name=="Bullet" or hit.Name=="Laser" or string.lower(hit.Name)=="water" or hit.Name=="Rail" then
			return true
		end
	end
	return false
end

function castray(startpos,vec,length,ignore,delayifhit)
	local hit,endpos2=game.Workspace:FindPartOnRay(Ray.new(startpos,vec*length),ignore)
	if hit~=nil then
		if checkintangible(hit) then
			if delayifhit then
				wait()
			end
			hit,endpos2=castray(endpos2+(vec*.01),vec,length-((startpos-endpos2).magnitude),ignore,delayifhit)
		end
	end
	return hit,endpos2
end

function drawbeam(beamstart,beamend,clr,fadedelay)
	local dist=(beamstart-beamend).magnitude
	local laser=Instance.new("Part")
	laser.Name="Effect"
	laser.Anchored=true
	laser.CanCollide=false
	laser.Shape="Block"
	laser.formFactor="Custom"
	laser.Size=Vector3.new(.2,.2,.2)
	laser.Transparency=5
	laser.Material=Enum.Material.Plastic
	laser.Locked=true
	laser.TopSurface=0
	laser.BottomSurface=0
	laser.BrickColor=clr
	laser.CFrame=CFrame.new(beamend,beamstart)*CFrame.new(0,0,-dist/2)*CFrame.Angles(math.pi/2,0,0)
	local m=Instance.new("SpecialMesh")
	m.Scale=Vector3.new(2,dist*5,2)
	m.Parent=laser
	debris:AddItem(laser,fadedelay*3)
	laser.Parent=game.Workspace
	--wait(.25)
	local frames=fadedelay/rate
	for frame=1,frames do
		wait(rate)
		local percent=frame/frames
		--m.Scale=Vector3.new(1+(20*percent),dist*5,1+(20*percent))
		laser.CFrame=laser.CFrame+windvec*rate
		laser.Transparency=.5+(percent*.5)
	end
	wait(2)
	laser:remove()
end

function onEquipped(mouse)
	equipped=true
end


script.Parent.Click.OnServerEvent:connect(function(client, hit)
	if client.Character == script.Parent.Parent then
		local he=sp.Parent:FindFirstChild("Head")
		local hu=sp.Parent:FindFirstChild("Humanoid")
		if check and he and hu and hu.Health>0 then
			check=false
			local startpos=he.Position
			local vec=(hit-startpos).unit
			local hit,endpos=castray(startpos,vec,range,sp.Parent,false)
			local fakevec=(endpos-(sp.Handle.CFrame*CFrame.new(barreloffset)).p).unit
			sp.PlaySound.Value=tick()
			local rail=Instance.new("Part")
			rail.Friction=1
			rail.Name="Rail"
			rail.Anchored=false
			rail.CanCollide=true
			rail.formFactor="Custom"
			rail.Size=Vector3.new(.6,.4,8)
			rail.TopSurface="Smooth"
			rail.BottomSurface="Smooth"
			rail.BrickColor=BrickColor.new("Dark stone grey")
			debris:AddItem(rail,10)
			if hit and hit~=nil then
				local h=hit.Parent:FindFirstChild("Humanoid")
				if h~=nil and h.Health>0 and GLib.IsTeammate(GLib.GetPlayerFromPart(script), GLib.GetPlayerFromPart(h))~=true then
					tagHumanoid(h)
					plr.leaderstats.Kills.Value = plr.leaderstats.Kills.Value + 1
					h:TakeDamage(damage)
				end
				if not hit.Anchored then
					rail.Velocity=fakevec*200
					if h==nil then
						hit:BreakJoints()
						hit.Velocity = vec*(hit:GetMass()+rail:GetMass())*10
					else
						hit.Velocity = vec*(hit:GetMass()+rail:GetMass())*10
					end
				end
				local newcf=CFrame.new(endpos,endpos+fakevec)*CFrame.new(0,0,(math.random()*(rail.Size.z-2))-((rail.Size.z-2)/2))
				rail.CFrame=newcf
				local w=Instance.new("Weld")
				w.Part0=hit
				w.Part1=rail
				w.C0=hit.CFrame:toObjectSpace(newcf)
				w.Parent=rail
				local soundscript=sp.SoundScript:clone()
				soundscript.Parent=rail
				soundscript.Disabled=false
				rail.Parent=game.Workspace
				if not hit.Anchored then
					hit.Velocity=hit.Velocity+fakevec*50
				end
			else
				rail.CFrame=CFrame.new(endpos,endpos+fakevec)
				rail.Velocity=fakevec*300
				rail.Parent=game.Workspace
			end
			delay(0,function()	--This is nessecery to fire multible shots at the same time. For those who don't know, delay() is a poor man's coroutine.
				drawbeam((sp.Handle.CFrame*CFrame.new(barreloffset)).p,endpos,BrickColor.new("Institutional white"),2)
			end)
			wait(firerate)
			check=true
		end
	end
end)

function onUnequipped()
	equipped=false
end

sp.Equipped:connect(onEquipped)
sp.Unequipped:connect(onUnequipped)

Alright, give me some times for me to fix this up

Alright how bout try this


local Debris = game:GetService("Debris")

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

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

sp=script.Parent
plr=game.Players.localPlayer

local GLib = require(206209239)

damage=100
firerate=1
range=1000
barreloffset=Vector3.new(0, 0.25, -1.7)
windvec=Vector3.new(2,-1,1).unit

rate=1/30
equipped=false
check=true

function checkintangible(hit)
	if hit and hit~=nil then
		if hit:IsDescendantOf(sp.Parent) or hit.Transparency>.8 or hit.Name=="Handle" or hit.Name=="Effect" or hit.Name=="Bullet" or hit.Name=="Laser" or string.lower(hit.Name)=="water" or hit.Name=="Rail" then
			return true
		end
	end
	return false
end

function castray(startpos,vec,length,ignore,delayifhit)
	local hit,endpos2=game.Workspace:FindPartOnRay(Ray.new(startpos,vec*length),ignore)
	if hit~=nil then
		if checkintangible(hit) then
			if delayifhit then
				wait()
			end
			hit,endpos2=castray(endpos2+(vec*.01),vec,length-((startpos-endpos2).magnitude),ignore,delayifhit)
		end
	end
	return hit,endpos2
end

function drawbeam(beamstart,beamend,clr,fadedelay)
	local dist=(beamstart-beamend).magnitude
	local laser=Instance.new("Part")
	laser.Name="Effect"
	laser.Anchored=true
	laser.CanCollide=false
	laser.Shape="Block"
	laser.formFactor="Custom"
	laser.Size=Vector3.new(.2,.2,.2)
	laser.Transparency=5
	laser.Material=Enum.Material.Plastic
	laser.Locked=true
	laser.TopSurface=0
	laser.BottomSurface=0
	laser.BrickColor=clr
	laser.CFrame=CFrame.new(beamend,beamstart)*CFrame.new(0,0,-dist/2)*CFrame.Angles(math.pi/2,0,0)
	local m=Instance.new("SpecialMesh")
	m.Scale=Vector3.new(2,dist*5,2)
	m.Parent=laser
	debris:AddItem(laser,fadedelay*3)
	laser.Parent=game.Workspace
	--wait(.25)
	local frames=fadedelay/rate
	for frame=1,frames do
		wait(rate)
		local percent=frame/frames
		--m.Scale=Vector3.new(1+(20*percent),dist*5,1+(20*percent))
		laser.CFrame=laser.CFrame+windvec*rate
		laser.Transparency=.5+(percent*.5)
	end
	wait(2)
	laser:remove()
end

function onEquipped(mouse)
	equipped=true
end


script.Parent.Click.OnServerEvent:connect(function(client, hit)
	if client.Character == script.Parent.Parent then
		local he=sp.Parent:FindFirstChild("Head")
		local hu=sp.Parent:FindFirstChild("Humanoid")
		if check and he and hu and hu.Health>0 then
			check=false
			local startpos=he.Position
			local vec=(hit-startpos).unit
			local hit,endpos=castray(startpos,vec,range,sp.Parent,false)
			local fakevec=(endpos-(sp.Handle.CFrame*CFrame.new(barreloffset)).p).unit
			sp.PlaySound.Value=tick()
			local rail=Instance.new("Part")
			rail.Friction=1
			rail.Name="Rail"
			rail.Anchored=false
			rail.CanCollide=true
			rail.formFactor="Custom"
			rail.Size=Vector3.new(.6,.4,8)
			rail.TopSurface="Smooth"
			rail.BottomSurface="Smooth"
			rail.BrickColor=BrickColor.new("Dark stone grey")
			debris:AddItem(rail,10)
			if hit and hit~=nil then
				local h=hit.Parent:FindFirstChild("Humanoid")
				if h~=nil and h.Health>0 and GLib.IsTeammate(GLib.GetPlayerFromPart(script), GLib.GetPlayerFromPart(h))~=true then
					UntagHumanoid(h)
					TagHumanoid(h, client)
					plr.leaderstats.Kills.Value = plr.leaderstats.Kills.Value + 1
					h:TakeDamage(damage)
				end
				if not hit.Anchored then
					rail.Velocity=fakevec*200
					if h==nil then
						hit:BreakJoints()
						hit.Velocity = vec*(hit:GetMass()+rail:GetMass())*10
					else
						hit.Velocity = vec*(hit:GetMass()+rail:GetMass())*10
					end
				end
				local newcf=CFrame.new(endpos,endpos+fakevec)*CFrame.new(0,0,(math.random()*(rail.Size.z-2))-((rail.Size.z-2)/2))
				rail.CFrame=newcf
				local w=Instance.new("Weld")
				w.Part0=hit
				w.Part1=rail
				w.C0=hit.CFrame:toObjectSpace(newcf)
				w.Parent=rail
				local soundscript=sp.SoundScript:clone()
				soundscript.Parent=rail
				soundscript.Disabled=false
				rail.Parent=game.Workspace
				if not hit.Anchored then
					hit.Velocity=hit.Velocity+fakevec*50
				end
			else
				rail.CFrame=CFrame.new(endpos,endpos+fakevec)
				rail.Velocity=fakevec*300
				rail.Parent=game.Workspace
			end
			delay(0,function()	--This is nessecery to fire multible shots at the same time. For those who don't know, delay() is a poor man's coroutine.
				drawbeam((sp.Handle.CFrame*CFrame.new(barreloffset)).p,endpos,BrickColor.new("Institutional white"),2)
			end)
			wait(firerate)
			check=true
		end
	end
end)

function onUnequipped()
	equipped=false
end

sp.Equipped:connect(onEquipped)
sp.Unequipped:connect(onUnequipped)

The script breaks the gun overall. Mind if I send you the tool model?

Maybe Not

oof, I think I could not help you. Sorry for wasting ur time and goodluck on fixing this

inhale

What is deaths?
What is “creator”?
Are there errors?
What are you trying to get with tag.Value?

Deaths is the count of how many deaths a player has had, in leaderstats.
Creator, have no idea since I followed a tutorial online.
There are no errors.
Tag.Value, also do not know.

The leaderstats work fine, but it’s an issue with the railgun since I’ve tested it with a sword and works fine with that.

you don’t seem to be tagging the humanoid. also, are you handling the damage on the server or the client?

are you using “LocalPlayer” in a server side script?

No, that’s just a random unused variable in the script.

Is this script on the server or the client?

Server

that’s very important, i’d recommend getting the player like this:

local player = tool:FindFirstAncestorWhichIsA("Player") -- while tool is in backpack, pre equipped state

...
-- railgun
tagHumanoid(humanoid, player)
humanoid:TakeDamage(damage)

What is localPlayer?

How is this not throwing an error?!

LocalPlayer doesn’t throw an error when used on a server script. It simply returns nil.

I have no idea. But this variable isn’t used in the script anywhere so it has no problem here

Big problem right here, you didn’t add player to the tagHumanoid function. You need this for it to add to the kills. As I mentioned earlier, make sure you change this:

…to this:

local plr = tool:FindFirstAncestorWhichIsA("Player")