Attempt to index nil with 'FindFirstChild'

Title explains this. I’m trying to add kills when they kill a player. Here is what I got in the output:

Here is my script:

function hitDetection(hit,ray, mousepos)
	local model = hit:FindFirstAncestorOfClass("Model")
	if model then
		local humanoid = model:FindFirstChildWhichIsA("Humanoid") or nil
		if humanoid then
			if hit.Name == "Head" then
				humanoid:TakeDamage(configuration.HeadShotDamage)
				local player = game.Players:GetPlayerFromCharacter(hit.Parent)
				if humanoid.Health == 0 then
					player.leaderstats.Kills.Value += 1
					player.RoundKills.Value += 1
				end
			else
				humanoid:TakeDamage(configuration.Damage)
				local player = game.Players:GetPlayerFromCharacter(model)
				if humanoid.Health == 0 then
					player:FindFirstChild("leaderstats").Kills.Value += 1
					player:FindFirstChild("RoundKills").Value += 1
				end
			end
		else
			return true
		end
	end
end

you are attempting to call :FindFirstChild on nil. thats what it means. im gonna assume player is nil as that is the only time i see :FindFirstChild being used

How can I actually get the player?

is player actually nil? if it is then what the heck is model? is model an NPC or something

Try changing

local player = game.Players:GetPlayerFromCharacter(model)

To

local player = game.Players:GetPlayerFromCharacter(hit.Parent)

he is using hit:FindFirstAncestorOfClass("Model") while also checking if theres a humanoid. so im pretty sure model is a character. plus if you do hit.Parent then what if hit is a child of an accessory?

1 Like

From what I can see, you’re getting the player from the character you hit and not the character who fired the shot. This could mean that kills are given to the wrong player, and also may be causing the error since I assume you’re trying to shoot an NPC that doesn’t have a player.

Yes I am shooting a NPC, I’ll give it a try with shooting a player.

Tested it, and it is still not working

Is there a way to get the player in a different way?