Attempt to index nil with 'FindFirstChild'

Basically everytime a part touches a character, this error appears for no reason, it’s not really affecting anything, its just annoying to see it pop up many times in the output. Do you have any idea to fix this?

There’s script

local DAMAGE = 47
local INTERVAL = 0.05

local touchingHumanoids = {}

script.Parent.Touched:Connect(function(hit)
	local humanoid = hit.Parent:FindFirstChild("Humanoid")
	if humanoid and hit.Parent ~= script.Parent.Parent.Creator.Value then
		touchingHumanoids[humanoid] = true
	end
end)

script.Parent.TouchEnded:Connect(function(hit)
	local humanoid = hit.Parent:FindFirstChild("Humanoid")
	if humanoid and hit.Parent ~= script.Parent.Parent.Creator.Value then
		touchingHumanoids[humanoid] = nil
	end
end)

while true do
	for humanoid in pairs(touchingHumanoids) do
		humanoid:TakeDamage(DAMAGE)
		
		if humanoid:FindFirstChild("BurnEffect") == nil then
			local BurnEffect = script.BurnEffect:Clone()
			BurnEffect.Parent = humanoid
			BurnEffect.Enabled = true
		elseif humanoid:FindFirstChild("BurnEffect") ~= nil then
			humanoid:FindFirstChild("BurnEffect"):Destroy()
			local BurnEffect = script.BurnEffect:Clone()
			BurnEffect.Parent = humanoid
			BurnEffect.Enabled = true
		end 
	end
	task.wait(INTERVAL)
end

image

1 Like

Pretty sure that this is going over the values in the table that you set to nil, table.remove would move the following values down by one index each, but setting an index to nil won’t do the same.

You can try printing the values that are being iterated through to see for yourself

my bad, it was the wrong error screenshot, here is the real one
image

In that case, try printing what hit is and what hit.Parent is

image
in the first print is “hit” and in second print is “hit.Parent”, i figured it out that this error appears only when character dies

Yeah when a character dies it gets destroyed, setting their Parent to nil, same happens with all their children, you could add a check like hit:IsDescendantOf(workspace) to make sure that the instance wasn’t destroyed yet

sadly, it is still not working.

Can i see what changes you made to the code?

well, i tried three variations, there is all of them



The first one should be the working one, it could be that the destroy and the check happen on the same frame, you could try yielding one frame at the start of the touch ended connection and see if that changes anything

excuse me, can you explain me what does yielding one frame, you mean typing task.wait()?

Yes exactly that, im assuming TouchEnded triggers because the character is being destroyed, which is happening right as the if case is happening too so it could be that it checks on the same frame as it’s being destroyed. Putting task.wait() right at the start of the function could give it enough time to fully destroy everything before you check if the Instance is a descendant of the workspace

Wait im completely stupid as to what you’re trying to do, you can’t reference the Humanoid anymore after the character was destroyed

sadly, still again doesn’t working, it now prints this error:
image

When the character is destroyed and the touch ends, the only reference you have to the humanoid is the table, meaning that you have a memory leak where the table is the only thing holding the instance. You would have to check the table on every touch end and see if the humanoid is a descendant of the workspace instead.

A weak table might be able to work too, but the garbage collection mostly happens delayed, so the Humanoid could still be in the table even after destroy is called

Its a bit of a tricky issue since the Humanoid isn’t a physical object, so you should try referencing the touching character models instead. This way you will be able to check if the Character is part of the workspace, and just reference downwards to the humanoid when you need to

just added if hit.Parent ~= nil then and it worked.

script.Parent.TouchEnded:Connect(function(hit)
	if hit.Parent ~= nil then
		local humanoid = hit.Parent:FindFirstChild("Humanoid")
		if humanoid and hit.Parent ~= script.Parent.Parent.Creator.Value then
			touchingHumanoids[humanoid] = nil
		end
	end
end)

thanks for help

This will still cause a memory leak. I have written your code a bit differently now:

local Players = game:GetService("Players")

local DAMAGE = 47
local INTERVAL = 0.05

local TouchingCharacters : {Model} = {}
setmetatable(TouchingCharacters, {__mode = "v"}) --Makes the table weak value-based, removing the touching characters automatically when they are collected by gc

local function IsCharacterTouching(Character : Model): boolean
	for _,Part in pairs(Character:GetChildren()) do
		if Part:IsA("BasePart") then
			local TouchingParts = Part:GetTouchingParts()
			if table.find(TouchingParts,script.Parent) then
				return true
			end
		end
	end
	return false --Fallback if no part is touching, or if the character doesn't have any BaseParts
end

script.Parent.Touched:Connect(function(hit)
	local Character = hit.Parent
	
	if Players:GetPlayerFromCharacter(Character) and not table.find(TouchingCharacters,Character) then
		table.insert(TouchingCharacters,Character)
	end
end)

script.Parent.TouchEnded:Connect(function(hit)
	local Character = hit.Parent
	
	if Players:GetPlayerFromCharacter(Character) then
		if not IsCharacterTouching(Character) then
			table.remove(TouchingCharacters,table.find(TouchingCharacters,Character))
		end
	end
end)

while true do
	for _,Character : Model in pairs(TouchingCharacters) do
		local humanoid = Character:FindFirstChild("Humanoid")
		
		if humanoid then
			humanoid:TakeDamage(DAMAGE)

			if humanoid:FindFirstChild("BurnEffect") == nil then
				local BurnEffect = script.BurnEffect:Clone()
				BurnEffect.Parent = humanoid
				BurnEffect.Enabled = true
			elseif humanoid:FindFirstChild("BurnEffect") ~= nil then
				humanoid:FindFirstChild("BurnEffect"):Destroy()
				local BurnEffect = script.BurnEffect:Clone()
				BurnEffect.Parent = humanoid
				BurnEffect.Enabled = true
			end 
		end
	end
	task.wait(INTERVAL)
end

what does memory leak mean, i heard it but actually dont know what is it