Attempt to index nil with 'FindFirstChild'

Can anyone explain to me why putting the kill function causes script to index nil? I put the error in the script. When you remove the kill function is works perfectly.

local function addPlayerToTable(player)
	playersTable[player.Name] = true -- Adds player to table
end

local function removePlayerFromTable(player)
	playersTable[player.Name] = nil -- Removes player from table
end

function Kill(Player, Humanoid)
	local KillCurrency = math.random(5, 25)

	if Humanoid and Humanoid.Health <= 0  then
		-- Check if player is already in the table
		if not playersTable[Player.Name] then
			-- Add player to the table
			addPlayerToTable(Player)

			-- Update player's stats
			Player.leaderstats:FindFirstChild("Kills").Value += 1
			Player.leaderstats:FindFirstChild("Credit").Value += KillCurrency

			-- Remove player from the table after updating stats
			task.wait(game.Players.RespawnTime)
			removePlayerFromTable(Player)
		end
	end
end
for i = 0, 30 do
			local Hits = {}
			local HitBox = workspace:GetPartBoundsInBox(CFrame.new((Data.StartPosition + Data.EndPosition)/2, Data.EndPosition), Vector3.new(8, 8, Data.Distance), Params)

			for Index,Part in pairs(HitBox) do
				if Hits[Part.Parent] == nil and Part.Parent:FindFirstChild("HumanoidRootPart") then --The error
					local HumanoidRootPart = Part.Parent:WaitForChild("HumanoidRootPart")

					if HumanoidRootPart and Part.Parent:GetAttribute("Blocking") == false then
						Hits[Part.Parent] = true
						
						BeamHit = Part.Parent
						
						BeamHit:SetAttribute("Stunned", true)

						Humanoid = Part.Parent:WaitForChild("Humanoid")

						local Damage = math.random(0, 30)
						local CurrencyMultiplier = Player.Character:GetAttribute("CurrencyMultiplier")
						local CurrencyGiven = Damage/10 * CurrencyMultiplier

						local HitAnimation = Instance.new("Animation", Part.Parent.HumanoidRootPart)
						local HitSound = SoundService.Combat:WaitForChild("Impact"):Clone()

						HitSound.Parent = workspace.SFX
						HitAnimation.AnimationId = Animations[1]

						local Hit = KatariFX:WaitForChild("Hit"):Clone()

						Humanoid:LoadAnimation(HitAnimation):Play()

						Hit.Parent = Part.Parent.HumanoidRootPart
						Hit.CFrame = Part.Parent.HumanoidRootPart.CFrame

						local Effects = coroutine.wrap(function()
							for i,v in pairs(Hit.Attachment:GetDescendants()) do
								if v:IsA("ParticleEmitter") then
									v.Enabled = true
									task.wait(0.01)
									v.Enabled = false
								end
							end
						end)

						Effects()

						HitSound:Play()
						Humanoid:TakeDamage(Damage)
						
					
						Kill(Player, Humanoid) --Only when this is inserted
						
						Player.leaderstats.Score.Value += Damage
						Player.leaderstats.Credit.Value += CurrencyGiven

						local BeamerUltimateCharge = math.random(0, 1)
						local CurrentUltimateValue = Player.Character:GetAttribute("UltimateValue")

						if Player.Character:GetAttribute("UltimateValue") < 100 then
							Player.Character:SetAttribute("UltimateValue", CurrentUltimateValue + BeamerUltimateCharge)
						elseif Player.Character:GetAttribute("UltimateValue") >= 100 then
							Player.Character:SetAttribute("UltimateValue", 100)
						end

						game.Debris:AddItem(HitSound, HitSound.TimeLength)
						game.Debris:AddItem(Hit, 1)
						game.Debris:AddItem(HitAnimation, 1)
					end
				elseif Hits[Part.Parent] == nil and Part.Parent:GetAttribute("Blocking") == true then
					local HumanoidRootPart = Part.Parent:FindFirstChild("HumanoidRootPart")

					if HumanoidRootPart then
						Hits[Part.Parent] = true

						local BlockThing = coroutine.wrap(function()
							Block(Part, Player)
						end)

						BlockThing()
					end
				end
			end
			task.wait(0.1)
		end

How and when are you adding the players to this table. Sometimes a player has already joined the server before the PlayerAdded connect fires, so you have to manually add the players.

1 Like

First you specifically check if Part.Parent is nil, and then attempt to use FindFirstChild on Part.Parent, which will always be nil under these circumstances, could you explain what you’re trying to do here?

It in the kill function, what do you mean?

Part.Parent isn’t nil, it refers to character that hitbox hit as the script shows.