Value is nil for some reason

Hi! I’m having yet another issue with roblox today as it thinks a value is nil when it really isn’t.

for index, descendant in pairs(descendants) do
	if descendant:IsA("Humanoid") then
		local NpcVal = descendant.Parent
		if NpcVal then
			print("-----")
			print(NpcVal)
			SetCollisions(NpcVal)
		end
	end
end

function SetCollisions(Part)
	for _,Prt in pairs(Part:GetChildren()) do
		if Prt:IsA("BasePart") or Prt:IsA("MeshPart") then
		PhysicsGet:SetPartCollisionGroup(Prt,"Plr")
		end
	end
end

it errors on SetCollisions(NpcVal) “Attempt to call a nil value”

Anyone know whats going on here?

Try creating your function earlier like so:


function SetCollisions(Part)
	for _,Prt in pairs(Part:GetChildren()) do
		if Prt:IsA("BasePart") or Prt:IsA("MeshPart") then
		PhysicsGet:SetPartCollisionGroup(Prt,"Plr")
		end
	end
end
for index, descendant in pairs(descendants) do
	if descendant:IsA("Humanoid") then
		local NpcVal = descendant.Parent
		if NpcVal then
			print("-----")
			print(NpcVal)
			SetCollisions(NpcVal)
		end
	end
end

Totally forgot I had to do that. Thanks!

1 Like

I knew that was the issue! In order to call your function, you need to create it first.