How can I store a problematic variable without it causing an error, inside of a metatable?

I have an array like so:

            array[1] = {}
            array[1].Type = "double"
            array[1].DoorMain1 = door:FindFirstChild("doorMain1")
            array[1].DoorMainPart1 = array[1].DoorMain1:FindFirstChild("doorMain")
            array[1].DoorMain2 = door:FindFirstChild("doorMain2")
            array[1].DoorMainPart2 = array[1].DoorMain2:FindFirstChild("doorMain")
            array[1].OpenAnimation1 = tweser:Create(array[1].DoorMainPart1, TweenInfo.new(1.5, Enum.EasingStyle.Quint, Enum.EasingDirection.Out), {CFrame = array[1].DoorMainPart1.CFrame + Vector3.new(0,0,-4)})
            array[1].OpenAnimation2 = tweser:Create(array[1].DoorMainPart2, TweenInfo.new(1.5, Enum.EasingStyle.Quint, Enum.EasingDirection.Out), {CFrame = array[1].DoorMainPart2.CFrame + Vector3.new(0,0,4)})
            array[1].CloseAnimation1 = tweser:Create(array[1].DoorMainPart1, TweenInfo.new(1.5, Enum.EasingStyle.Quint, Enum.EasingDirection.Out), {CFrame = array[1].DoorMainPart1.CFrame + Vector3.new(0,0,4)})
            array[1].CloseAnimation2 = tweser:Create(array[1].DoorMainPart2, TweenInfo.new(1.5, Enum.EasingStyle.Quint, Enum.EasingDirection.Out), {CFrame = array[1].DoorMainPart2.CFrame + Vector3.new(0,0,-4)})
            array[2] = {}
            array[2].Type = "fridge"
            array[2].DoorMain1 = door:FindFirstChild("doorMain1")
            array[2].DoorMainPart1 = array[2].DoorMain1:FindFirstChild("doorMain")
            array[2].OpenAnimation1 = tweser:Create(array[2].DoorMainPart1, TweenInfo.new(1.5, Enum.EasingStyle.Quint, Enum.EasingDirection.Out), {CFrame = array[2].DoorMainPart1.CFrame + Vector3.new(0,0,4)})
            array[2].CloseAnimation1 = tweser:Create(array[2].DoorMainPart1, TweenInfo.new(1.5, Enum.EasingStyle.Quint, Enum.EasingDirection.Out), {CFrame = array[2].DoorMainPart1.CFrame + Vector3.new(0,0,-4)})

Not all door types have a DoorMain2 part, so occasionally the :FindFirstChild() will return nil. This isn’t a problem on it’s own, but because afterwards we use the variable to check for doorMain, and we can’t find the first child of nil, this causes an error if we interact with a door without a 2nd DoorMain. I’ve been trying to implement a way to return only the index that is needed, using __index, but I how can I store the variables to be returned from the metatable if the variables cause an error?

    local _ = pcall(function()
    array[1].DoorMain2 = door:FindFirstChild("doorMain2")
    array[1].DoorMainPart2 = array[1].DoorMain2:FindFirstChild("doorMain")
    array[1].OpenAnimation2 = tweser:Create(array[1].DoorMainPart2, TweenInfo.new(1.5, Enum.EasingStyle.Quint, Enum.EasingDirection.Out), {CFrame = array[1].DoorMainPart2.CFrame + Vector3.new(0,0,4)})
    array[1].CloseAnimation2 = tweser:Create(array[1].DoorMainPart2, TweenInfo.new(1.5, Enum.EasingStyle.Quint, Enum.EasingDirection.Out), {CFrame = array[1].DoorMainPart2.CFrame + Vector3.new(0,0,-4)})
    end)

pcall() seems to work well enough in this scenario. This isn’t a perfect fix, and it’s not how I expected to fix it, but it does get the job done. I’ll leave this open for anyone who wants to share a different way to do this with metatables.

Rather than metatables, it seems like you need DoorMain to be another array that you can loop over later in the code to access any DoorMain components.

1 Like