Having trouble getting the player that killed

I am trying to make a cash per kill leaderboard. I think the problem is the player variable.

local bullet = script.Parent
local headshot_sound = game.Workspace.HEADSHOT

local function player_check(otherPart)

	local humanoid = otherPart.Parent:FindFirstChild('Humanoid')
	local shooter = game.Players:FindFirstChild(bullet.Attacker.Value)
	local player = game.Players:FindFirstChild(humanoid.Parent.Name)
	local killerTag = game.Players:FindFirstChild(player.Killer.Value)

	if humanoid and otherPart.Parent.Name ~= bullet.Attacker.Value then
		if player.TeamColor ~= shooter.TeamColor and game.Players:GetPlayerFromCharacter(otherPart.Parent).TeamColor ~= shooter.TeamColor then
			humanoid:TakeDamage(0)
			if shooter.InstaKill.Value == true then
				humanoid:TakeDamage(999)
			else
				if otherPart.Name == 'Head' then
					humanoid:TakeDamage(100)
					headshot_sound:Play()
				elseif otherPart.Name == "HumanoidRootPart" then
					humanoid:TakeDamage(30)
				else
					humanoid:TakeDamage(15)
				end
			end
		else
			if player then
				local tag = player:FindFirstChild('Killer')
				if tag then
					tag.Value = bullet.Attacker.Value
				end
			end
		end
	end
	bullet:Destroy()
end

bullet.Touched:Connect(player_check)

2 Likes

Its not adding kills to the leaderboard.

Leaderboard script

local function onPlayerJoin(player)
local leaderstats = Instance.new(“Folder”)
leaderstats.Name = ‘leaderstats’
leaderstats.Parent = player

local kills = Instance.new("IntValue")
kills.Value = 0
kills.Name = 'Kills'
kills.Parent = leaderstats

local streak = Instance.new('IntValue')
streak.Value = 0
streak.Name = 'Streak'
streak.Parent = player

local deaths = Instance.new('IntValue')
deaths.Value = 0
deaths.Name = 'Deaths'
deaths.Parent = leaderstats

local cash = Instance.new('IntValue')
cash.Value = 100
cash.Name = 'Cash'
cash.Parent = leaderstats

local killer = Instance.new('StringValue')
killer.Name = 'Killer'
killer.Parent = player

local instaKill = Instance.new('BoolValue')
instaKill.Name = 'InstaKill'
instaKill.Value = false
instaKill.Parent = player

end

game.Players.PlayerAdded:Connect(onPlayerJoin)

–Death Tracker

game:GetService(‘Players’).PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
character:WaitForChild(“Humanoid”).Died:Connect(function()

		player.leaderstats.Deaths.Value = player.leaderstats.Deaths.Value + 1

		player.Streak.Value = 0

		local killerTag = game.Players:FindFirstChild(player.Killer.Value)
		if killerTag then
			killerTag.leaderstats.Kills.Value = killerTag.leaderstats.Kills.Value + 1
			killerTag.leaderstats.Cash.Value = killerTag.leaderstats.Cash.Value + 50
			killerTag.Streak.Value = killerTag.Streak.Value + 1
		end
	end)
end)

end)

Its not adding kills to the leaderboard.

Do you get any errors in the output?
The problem may be that killerTag is still set to nil. To check this, print(killerTag) right after you define the variable.

2 Likes

It is nil. Why is it nil though? No errors in output.

I believe the problem is that you’re not setting the killerTag value when a player is killed, to do this you could do something like this

if player.TeamColor ~= shooter.TeamColor and game.Players:GetPlayerFromCharacter(otherPart.Parent).TeamColor ~= shooter.TeamColor then
		local damage = 0
		if shooter.InstaKill.Value == true then
			damage = humanoid.Health
		else
			if otherPart.Name == 'Head' then
				damage = 100
				headshot_sound:Play()
			elseif otherPart.Name == "HumanoidRootPart" then
				damage = 30
			else
				damage = 15
			end
		end
		local finalHP = humanoid.Health - damage
		if finalHP <= 0 then
			local tag = player:FindFirstChild('Killer')
			if tag then
				tag.Value = bullet.Attacker.Value
			end
		end
		humanoid.Health = finalHP
	else
		if player then
			local tag = player:FindFirstChild('Killer')
			if tag then
				tag.Value = bullet.Attacker.Value
			end
		end
	end
1 Like