For some reason model == nil

Hi! So I am working on a viewport frame inventory system and I sadly have a bug. It would work perfectly fine but after I added code in a different script that would add different pets in the frames other than test objects I get an error. It reads, " 13:48:38.350 - Players.Wizzardofaz.PlayerGui.PetInventory.Frame.ImageLabel.ScrollingFrame.PetFrame.ViewportFrame.LocalScript:18: attempt to index nil with ‘GetBoundingBox’" After printing out model it returned nil. Here is the code:

local vpf = script.Parent
local items = script.Parent:WaitForChild("Items")
local children = items:GetChildren()

         local index, event = 1, nil

    local camera = Instance.new("Camera")
   camera.FieldOfView = 10
vpf.CurrentCamera = camera

vpf.Visible = true

– rotation

local function setRotationEvent(model)
local currentAngle = 0
print(model)
local modelCF, modelSize = model:GetBoundingBox()	

local rotInv = (modelCF - modelCF.p):inverse()
modelCF = modelCF * rotInv
modelSize = rotInv * modelSize
modelSize = Vector3.new(math.abs(modelSize.x), math.abs(modelSize.y), math.abs(modelSize.z))

local diagonal = 0
local maxExtent = math.max(modelSize.x/2, modelSize.y/2, modelSize.z/2)
local tan = math.tan(math.rad(camera.FieldOfView/2))

if (maxExtent == modelSize.x) then
	diagonal = math.sqrt(modelSize.y*modelSize.y + modelSize.z*modelSize.z)/2
elseif (maxExtent == modelSize.y) then
	diagonal = math.sqrt(modelSize.x*modelSize.x + modelSize.z*modelSize.z)/2
else
	diagonal = math.sqrt(modelSize.x*modelSize.x + modelSize.y*modelSize.y)/2
end

local minDist = (maxExtent/2)/tan + diagonal

return game:GetService("RunService").RenderStepped:Connect(function(dt)
	currentAngle = currentAngle + 1*dt*60
	camera.CFrame = modelCF * CFrame.fromEulerAnglesYXZ(0, math.rad(currentAngle), 0) * 
CFrame.new(0, 0, minDist + 3)
end)
end

local function setIndexActive(newIndex)
	if (event) then
    	event:Disconnect()
	    children[index].Parent = items
	end

 index = newIndex
event = setRotationEvent(children[index])
children[index].Parent = vpf
end

--

setIndexActive(index)

vpf.InputBegan:Connect(function(input)
if (input.UserInputType == Enum.UserInputType.MouseButton1) then
	setIndexActive(index + 1 > #children and 1 or index + 1)
end
end)

Here is the script I used to summon the pet:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local DataStore2 = require(1936396537)

game.Players.PlayerAdded:Connect(function(player)
local ownedPets = DataStore2("pets", player)
local petTable = ownedPets:GetTable({})

for i,v in pairs(petTable) do
	local clonedPet = ReplicatedStorage.PetPack[v]:Clone()
	clonedPet.Parent = player.Character
	clonedPet.PrimaryPart.CFrame = CFrame.new(0,0,0)
	print("Cloned "..v.."!")
	
	local clonedViewPort = ReplicatedStorage:WaitForChild("PetFrame"):Clone()
	clonedViewPort.Parent = player.PlayerGui:WaitForChild("PetInventory").Frame.ImageLabel.ScrollingFrame
	clonedViewPort:WaitForChild("ViewportFrame").Pet.Value = v
	local clonedPet = ReplicatedStorage.PetPack[v]:Clone()
	clonedPet.Script:Destroy()
	clonedPet.Parent = clonedViewPort:WaitForChild("ViewportFrame").Items
end
end)

It might be that the argument you are passing to setRotationEvent isn’t actually a Model, but something else. Try calling “print(children[index]:IsA(“Model”))” before running setRotatiomEvent, and confirm that it prints true. If calling IsA throws a error, then it probably means “children[index]” is nil altogether.

1 Like