How to check if something exists?

Hit.Parent:FindFirstChild("Humanoid") 

Doesnt work bc then I get the error code Attempt to index nil with FindFirstChild.
Should i put it in a pcall?

1 Like

Hello,
Could you clarify to us where you get Hit from?

if Hit.Parent:FindFirstChild("Humanoid") then
-- Instance exists!
else
-- Instance doesn't exist.
end
1 Like

The error is already in the first line

Yes, sorry didn’t really read your post when i posted the script : /. The same as @PixelBytezz said. Where did you get .Hit from?

From a touched Event but I forgot to write it here

Okay, please post the full .Touched event so i can help fix the problem.

This just means that Hit.Parent is nil. You should check that Hit.Parent is not nil first by doing:

if (Hit.Parent) then
    if (Hit.Parent:FindFirstChild("Humanoid") then
        -- code
    end
end

Although I’m not sure why Hit.Parent would be nil, it would be interesting to see your full script.

1 Like

Problem happens here with :HasTag

local HitBox = {}
function HitBox.new(character, size, offset, damage, HitBoxTime, Waiting,Attack) -- creates hitbox
	local hrp = character:FindFirstChild("HumanoidRootPart")
	if hrp == nil then return end
	local weld = Instance.new("WeldConstraint", hrp)
	if Waiting >= 0 then task.wait(Waiting) end -- Delay
	local hitbox = Instance.new("Part")
	weld.Part0 = hrp
	weld.Part1 = hitbox

	hitbox.CanCollide = false
	hitbox.CanQuery = true
	hitbox.Massless = true
	hitbox.Transparency = 0.5
	hitbox.Name = "HitBox"
	hitbox:AddTag(Attack)
	hitbox:SetAttribute("damage", damage)

	hitbox.Size = size
	hitbox.CFrame = hrp.CFrame + hrp.CFrame.LookVector * offset.X + Vector3.new(0,offset.Y)
	hitbox.Parent = character

	hitbox.Touched:Connect(function(hit)
		local Humanoid = hit.Parent:FindFirstChild("Humanoid")
		local NPC =	hit.Parent:FindFirstChild("Humanoid"):HasTag("NPC")
		
		if Humanoid == nil or NPC then return end 


		for _, v in pairs(hitbox:GetChildren()) do
			if v:IsA("ObjectValue") then
				if v.Value == hit.Parent then return end 
			end
		end

		local hitCounter = Instance.new("ObjectValue", hitbox)
		hitCounter.Value = hit.Parent
		hit.Parent.Humanoid:TakeDamage(damage)
	end)
	game.Debris:AddItem(hitbox, HitBoxTime)
end
return HitBox

Nevermind what i said forgot you were the original poster, no idea how tough.

Try adding a check for Humanoid:

hitbox.Touched:Connect(function(hit)
		if not hit.Parent:FindFirstChild("Humanoid") then return end

Wait i wanted to write to check if something doesnt exist

For this line to work (:HasTag("NPC") it has to first find a humanoid. If it doesn’t then it will error:

local NPC = hit.Parent:FindFirstChild(“Humanoid”):HasTag(“NPC”)

So first check if the humanoid exists. If it does not, then return so the script does not error.

	hitbox.Touched:Connect(function(hit)
		if not hit:FindFirstChild("Humanoid") then return end

After getting the humanoid variable you should return if it is nil

I’ll assume that your script is inside the part that has the touch event in.
Hopefully this helps:

script.Parent.Touched:Connect(function(hit)
    if not Hit.Parent then return end
    if Hit.Parent:FindFirstChildOfClass("Humanoid") then
      -- Found player
    end
end)

i like how this and

are already the correct solutions, yet this post is still unsolved lol

didn’t have any time to programm bc I needed to study.
But it works inside a pcall.

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