Weird 'attempt to index nil with findfirstchild' error with my blood module

Hi, I have just created a decently functional blood emission module, however console gives me an error after a while of emitting the blood on the client. The error lists
ReplicatedStorage.Modules.BloodModule:20: attempt to index nil with ‘FindFirstChild’
I’m not really sure how this problem is even being provoked. Any help?

local BloodModule = {}
-- // Variables
local TweenService = game:GetService('TweenService')
local ReplicatedStorage = game:GetService('ReplicatedStorage')
local Misc = game:GetService('ReplicatedStorage'):WaitForChild('Misc')
local BloodItems = Misc:WaitForChild('BloodItems')
local Decals = BloodItems:WaitForChild('Decals')

-- // Main
function BloodModule.EmitBlood(Origin, Angle, Lifetime, DelayBeforeFade)
	local Blood = BloodItems:WaitForChild('Blood'):Clone()
	Blood.Parent = workspace:WaitForChild('Debris')
	Blood.Position = Origin.Position
	Blood.Trail.Color = ColorSequence.new(Blood.Color)
	local PossibleAngles = {-Angle, Angle}
	local Velocity = Vector3.new(math.random(-2,2),PossibleAngles[math.random(1,#PossibleAngles)],math.random(-2,2)).Unit
	Blood:ApplyImpulse(Velocity)
	
	Blood.Touched:Connect(function(Hit)
		if Hit ~= Origin and Hit.Parent ~= workspace:WaitForChild('Debris') and not Hit.Parent:FindFirstChild('Humanoid') and Hit ~= nil then
			Blood.Anchored = true
			Blood.CanCollide = false
			Blood.CanTouch = false
			Blood.Position+=Vector3.new(0,2,0)
			
			local RayParams = RaycastParams.new()
			RayParams.FilterType = Enum.RaycastFilterType.Exclude
			RayParams.FilterDescendantsInstances = {workspace:WaitForChild('Debris')}
			local RayCheckForValidBloodSpot = workspace:Raycast(Blood.Position, Vector3.new(0,-5,0), RayParams)
			if RayCheckForValidBloodSpot then
				local DecalPart = BloodItems:WaitForChild('Template'):Clone()
				DecalPart.Parent = workspace.Debris
				DecalPart.Position = RayCheckForValidBloodSpot.Position
				DecalPart.Anchored = true
				DecalPart.CanCollide = false
				local DecalsChildren = Decals:GetChildren()
				local RandomDecal = DecalsChildren[math.random(1, #DecalsChildren)]:Clone()
				RandomDecal.Parent = DecalPart
				if DelayBeforeFade<Lifetime then -- Failsafe in case delay is invalid over lifetime
					task.delay(DelayBeforeFade,function()
					TweenService:Create(RandomDecal, TweenInfo.new(Lifetime-DelayBeforeFade),{Transparency=1}):Play()
					end)
				end
				game.Debris:AddItem(DecalPart, Lifetime)
			end
		end
	end)
	game.Debris:AddItem(Blood, Lifetime)
end

return BloodModule

The error is happening because Hit does not have a parent (so it was likely destroyed). To fix the error you can check whether Hit.Parent is nil or not before doing any operations with it.

1 Like

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