Attempt to index nil with Parent

HI, im trying to make a case/lootbox system which goes through the items it could pick until it selects one. however, when trying to make the viewport frame of the items, it works once then i recieve the error and it breaks

local function getItem(ItemTable)
	local randomNumber = math.random(returnWeight(ItemTable))
	for i, entry in ipairs(ItemTable) do
		if randomNumber <= entry.Weight then
			return entry.item
		else
			randomNumber = randomNumber - entry.Weight
		end
	end
end

script.Parent.TextButton.MouseButton1Click:Connect(function()
	if player.leaderstats.Cash.Value >= script.Parent.price.Value then
		game.ReplicatedStorage.Cases.BuyCase:FireServer(player)
		if debounce == false then
			script.Parent.jp.Frame.Rarity.TextColor3 = Color3.new(255, 255, 255)
			script.Parent.jp.Frame.title.Text = "None"
			debounce = true
			
			for count = 1,40 do
				game.SoundService.Click:Play()
				local items5 = game.ReplicatedStorage.Items.Free
				local items1 = getItem(ItemTable)
				local Rarity = items1.Rarity
				local n1 = items1.Name
				script.Parent.jp.Frame.ViewportFrame:ClearAllChildren()
				local image = items5:FindFirstChild(n1)
				image.Parent = script.Parent.jp.Frame:WaitForChild("ViewportFrame") -- problematic line
				task.wait(0.1) 
				local camera = Instance.new("Camera")
				camera.CFrame = CFrame.new(image.Handle.Position + (image.Handle.CFrame.lookVector * 3),image.Handle.Position)
				camera.Parent = script.Parent.jp.Frame.ViewportFrame
				script.Parent.jp.Frame.ViewportFrame.CurrentCamera = camera 
				script.Parent.jp.Frame.title.Text = items1.Name
				script.Parent.jp.Frame.Rarity.Text = items1.Rarity
				
				for i, v in ipairs(colour) do
					if v.Rarity == Rarity then 
						script.Parent.jp.Frame.Rarity.TextColor3 = v.Color
					end
				end
			task.wait(0.1)	
			end
		
		local items2 = getItem(ItemTable)
		local name = items2.Name
			local image = items2:Clone()
			image.Parent = script.Parent.jp.Frame.ViewportFrame

			local camera = Instance.new("Camera")
			camera.CFrame = CFrame.new(image.Handle.Position + (image.Handle.CFrame.lookVector * 3),image.Handle.Position)
			camera.Parent = script.Parent.jp.Frame.ViewportFrame
			script.Parent.jp.Frame.ViewportFrame.CurrentCamera = camera 
		local rarity = items2.Rarity
		script.Parent.jp.Frame.Rarity.Text = rarity
		script.Parent.jp.Frame.title.Text = name
		game.SoundService["Ding Ding!"]:Play()
		game.ReplicatedStorage.Cases.GetItem:FireServer(name, rarity)
		
		for i, v in ipairs(colour) do
			if v.Rarity == rarity then
				script.Parent.jp.Frame.Rarity.TextColor3 = v.Color
			end
		end
		task.wait(cooldown)
		debounce = false
		end
	else
		
end
end)

image.Parent = script.Parent.jp.Frame:WaitForChild("ViewportFrame")
I believe this might be the issue. For this case, you can’t directly parent a game object, like a part, to a ViewportFrame. You should clone the object, so that the initial template part exists, and it can run multiple times.

1 Like

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