Error even though its in an If statement? Why?

So I have this code that tells if a a Object value has a value, if it doesnt then it should just carry on but there is still a error! Why?

		for _, rig in pairs(rigs) do
			local anims = rig:FindFirstChild('Anims')
			
			if anims:FindFirstChildOfClass('ObjectValue').Value then
				animsToUse[rig.Name] = anims:FindFirstChildOfClass('ObjectValue').Value
			elseif anims:GetChildren()[1] then
				animsToUse[rig.Name] = anims:GetChildren()[1]
			else
				warn('No ANIM TO USE FOR '..rig.Name)
			end
		end

If the object is not found, :FindFirstChild() will return nil, therefore attempting to index nil with Value, first check if the ObjectValue exists, then check for the value, to enaure the item actually exists, and then the object you are trying to get from it exists.

2 Likes

Is the error your warn(...)?
If not what are you getting in the Output window?
Try this to show you exactly what does or doesn’t triggerthe if so you can see if you are actually getting the items you think you are:

		if anims:FindFirstChildOfClass('ObjectValue').Value then
            print(anims:FindFirstChildOfClass('ObjectValue').Value)
			animsToUse[rig.Name] = anims:FindFirstChildOfClass('ObjectValue').Value
		elseif anims:GetChildren()[1] then
            print(anims:GetChildren()[1] )
			animsToUse[rig.Name] = anims:GetChildren()[1]
		else
			warn('No ANIM TO USE FOR '..rig.Name)
		end

EDIT Lol, basically what @DasKairo said.

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