Attempt to Index nil with 'FindFirstChild' - HitBox Issue

I’m having an error when where the hitbox doesn’t detect an enemyhumanoid it’ll give be an error saying “Index nil with ‘FindFirstChild’”.
How do I fix this issue?

Video:

local CombatRemote = game:GetService("ReplicatedStorage"):WaitForChild("CombatRemoteFolder"):WaitForChild("CombatRemote")
local RunService = game:GetService("RunService")


CombatRemote.OnServerEvent:Connect(function(player, tool, data, HakiBar)
	local DamageVFX = game:GetService("ReplicatedStorage").VFX["Damage/Debris"]
	local BloodHit = DamageVFX.BloodHit:Clone()
	local Blade = tool:FindFirstChild("Blade")
	local Data2 = player.Data2 
	if Data2 == nil then return
		
	end
	local HakiLevel = player.Data2.Haki
	local Character = player.Character
	local HumanoidRootPart = Character.HumanoidRootPart
	local Con
	
	

	local Hitboxfolder = Instance.new("Folder")
	Hitboxfolder.Name = "Hitbox"
	Hitboxfolder.Parent = workspace
	
	
	local Hitbox = Instance.new("Part")
	Hitbox.Name = "Hitbox"
	Hitbox.CFrame = HumanoidRootPart.CFrame * CFrame.new(0,0,-5)
	Hitbox.Size = Vector3.new(5, 5, 5)
	Hitbox.BrickColor = BrickColor.new("Red flip/flop")
	Hitbox.Transparency = 0.7
	Hitbox.CanCollide = false
	Hitbox.Massless = true
	Hitbox.Anchored = true
	

	
	
	local ConfirmTarget = {}
	
	for i, v in pairs(workspace:GetPartBoundsInBox(Hitbox.CFrame * CFrame.new(0,0,-2),Hitbox.Size )) do
		
	
		
		if v.Parent:FindFirstChild("Humanoid") ~= nil and v.Parent ~= nil and v.Parent ~= Character  then
			
				
				Hitbox.Parent = Hitboxfolder
			
			local EnemyHumanoid = v.Parent:FindFirstChild("Humanoid")
			local EHumanoidRootPart = v.Parent:FindFirstChild("HumanoidRootPart")
			if ConfirmTarget then
				if  not ConfirmTarget[EnemyHumanoid] then
					ConfirmTarget[EnemyHumanoid] = true
					
					if data.Form == "HakiEnabled" then
						EnemyHumanoid:TakeDamage(5 + HakiLevel.Value / 2)
						
						print(EnemyHumanoid.Health)
					else
						EnemyHumanoid:TakeDamage(5)
					end
				
					
					
					local Attachment = Instance.new("Attachment", EHumanoidRootPart)
					Attachment.Name = "BloodAttachment"
					Attachment.Parent = EHumanoidRootPart

					BloodHit.Parent = EHumanoidRootPart
					BloodHit:Clear()
					task.wait()
					BloodHit:Emit(5)

					local sound = Instance.new("Sound", EHumanoidRootPart)
					sound.SoundId = "rbxassetid://3939937734"

					sound:Play()
					task.wait(1)
					Attachment:Destroy()
					sound:Destroy()
				end
			end
		end
			
			
		
	end
	
	game.Debris:AddItem(Hitboxfolder,.1)
	
	
	
	
	
	end)
	
	```
1 Like

check the parent before checking the humanoid first probably

local CombatRemote = game:GetService("ReplicatedStorage"):WaitForChild("CombatRemoteFolder"):WaitForChild("CombatRemote")
local RunService = game:GetService("RunService")


CombatRemote.OnServerEvent:Connect(function(player, tool, data, HakiBar)
	local DamageVFX = game:GetService("ReplicatedStorage").VFX["Damage/Debris"]
	local BloodHit = DamageVFX.BloodHit:Clone()
	local Blade = tool:FindFirstChild("Blade")
	local Data2 = player.Data2 
	if Data2 == nil then return

	end
	local HakiLevel = player.Data2.Haki
	local Character = player.Character
	local HumanoidRootPart = Character.HumanoidRootPart
	local Con



	local Hitboxfolder = Instance.new("Folder")
	Hitboxfolder.Name = "Hitbox"
	Hitboxfolder.Parent = workspace


	local Hitbox = Instance.new("Part")
	Hitbox.Name = "Hitbox"
	Hitbox.CFrame = HumanoidRootPart.CFrame * CFrame.new(0,0,-5)
	Hitbox.Size = Vector3.new(5, 5, 5)
	Hitbox.BrickColor = BrickColor.new("Red flip/flop")
	Hitbox.Transparency = 0.7
	Hitbox.CanCollide = false
	Hitbox.Massless = true
	Hitbox.Anchored = true




	local ConfirmTarget = {}

	for i, v in pairs(workspace:GetPartBoundsInBox(Hitbox.CFrame * CFrame.new(0,0,-2),Hitbox.Size )) do



		if v.Parent and v.Parent:FindFirstChild("Humanoid") and v.Parent ~= Character  then


			Hitbox.Parent = Hitboxfolder

			local EnemyHumanoid = v.Parent:FindFirstChild("Humanoid")
			local EHumanoidRootPart = v.Parent:FindFirstChild("HumanoidRootPart")
			if ConfirmTarget then
				if  not ConfirmTarget[EnemyHumanoid] then
					ConfirmTarget[EnemyHumanoid] = true

					if data.Form == "HakiEnabled" then
						EnemyHumanoid:TakeDamage(5 + HakiLevel.Value / 2)

						print(EnemyHumanoid.Health)
					else
						EnemyHumanoid:TakeDamage(5)
					end



					local Attachment = Instance.new("Attachment", EHumanoidRootPart)
					Attachment.Name = "BloodAttachment"
					Attachment.Parent = EHumanoidRootPart

					BloodHit.Parent = EHumanoidRootPart
					BloodHit:Clear()
					task.wait()
					BloodHit:Emit(5)

					local sound = Instance.new("Sound", EHumanoidRootPart)
					sound.SoundId = "rbxassetid://3939937734"

					sound:Play()
					task.wait(1)
					Attachment:Destroy()
					sound:Destroy()
				end
			end
		end



	end

	game.Debris:AddItem(Hitboxfolder,.1)





end)
1 Like

Thanks for the help! it’s not giving me the error anymore.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.