Help With Viewportframe

Hello guys,

I have the problem that my pets that I program are not displayed in the ViewportFrame and I have no idea how to fix this, I have named the frame PetView and coded everything so, the console also gives no errors and yet it does not work. I ask that someone can help me or even fix the code (if that’s what it is). All important things can be found in the attachments:

local Player = game.Players.LocalPlayer
local Pets = Player:WaitForChild("Pets")
local Data = Player:WaitForChild("Data")
local UIelements = Player.PlayerGui:WaitForChild("UIelements")
local RS = game.ReplicatedStorage
local InventoryFrame = script.Parent.Inventory

local StorageText = script.Parent.StorageText
local EquippedText = script.Parent.Parent.PetButton.Max
function getLevel(totalXP)
	local Increment = 0
	local RequiredXP = 100
	for i = 0, RS.Pets.Settings.MaxPetLevel.Value do
		RequiredXP = 100 + (25*i)
		if totalXP >= (100*i) + Increment then
			if i ~= RS.Pets.Settings.MaxPetLevel.Value then
				if totalXP < ((100*i) + Increment) + RequiredXP then
					return i
				end
			else
				return i
			end
		end
		Increment = Increment+(i*25)
	end
end

function GetFolderFromPetID(PetID)
	for i,v in pairs(Player.Pets:GetChildren()) do
		if v.PetID.Value == PetID then
			return v
		end
	end
	return nil
end

function getLayoutOrder(PetID)
	local Folder = GetFolderFromPetID(PetID)
	local RarityNumber = RS.Pets.Rarities:FindFirstChild(RS.Pets.Models:FindFirstChild(Folder.Name).Settings.Rarity.Value).Order.Value
	local TotalLevels = RS.Pets.Settings.MaxPetLevel.Value
	local PetLevel = getLevel(Folder.TotalXP.Value)
	local Pets = {}
	local Counter = 0
	for i,v in pairs(RS.Pets.Models:GetChildren()) do
		Pets[#Pets + 1] = v
	end
	table.sort(Pets,function(a,b)
		return a:GetFullName() < b:GetFullName()
	end)
	for i,v in pairs(Pets) do
		if Folder.Equipped.Value == true then
			if Folder.Name == v.Name then
				return TotalLevels * #Pets * (#RS.Pets.Rarities:GetChildren() - RarityNumber) + (Counter * TotalLevels) + (TotalLevels - PetLevel)
			end
		else
			if Folder.Name == v.Name then
				return (#Pets * #RS.Pets.Rarities:GetChildren() * TotalLevels) + TotalLevels * #Pets * (#RS.Pets.Rarities:GetChildren() - RarityNumber) + (Counter * TotalLevels) + (TotalLevels - PetLevel)
			end
		end
		Counter = Counter + 1
	end
end

function newUI(PetFolder)
	local UI = UIelements.PetTemplate:Clone()
	local RSfolder = RS.Pets.Models:FindFirstChild(PetFolder.Name)
	local ModelClone = RSfolder:FindFirstChild(PetFolder:WaitForChild("Type").Value):Clone()
	local Gradient1 = RS.Pets.Rarities:FindFirstChild(RSfolder.Settings.Rarity.Value).Color.Value
	local Gradient2 = Color3.new(Gradient1.R - 150/255, Gradient1.G - 150/255, Gradient1.B - 150/255)
	local Camera = Instance.new("Camera", UI.PetView)
	local Pos = ModelClone.PrimaryPart.Position
	UI.PetID.Value = PetFolder:WaitForChild("PetID").Value
	UI.Name = PetFolder.Name
	UI.Equip.Value = PetFolder.Equipped.Value
	UI.Level.Value = getLevel(PetFolder:WaitForChild("TotalXP").Value)
	UI.UIGradient.Color = ColorSequence.new(Gradient1, Gradient2)
	UI.PetView.CurrentCamera = Camera
	ModelClone.Parent = UI.PetView
	Camera.CFrame = CFrame.new(Vector3.new(Pos.X + -3, Pos.Y, Pos.Z + -1), Pos)
	UI.Parent = InventoryFrame
	InventoryFrame.CanvasSize = UDim2.new(0, 0, 0, InventoryFrame.UIGridLayout.AbsoluteContentSize.Y + 200)
	UI.EquipScript.Disabled= false UI.EquipMarker.LocalScript.Disabled = false
	UI.LoadingMarker.LocalScript.Disabled = false
	UI.MultiDelete.Disabled = false
	UI.Stronge.LocalScript.Enabled = true
	UI.Stronge.Text = PetFolder.Multiplier1.Value
	if PetFolder.Equipped.Value == true then 
		UI.EquipMarker.Visible = true
	else
		UI.EquipMarker.Visible = false
	end
	
	for i,v in pairs(InventoryFrame:GetChildren()) do
		if not v:IsA("UIGridLayout") then
			v.LayoutOrder = getLayoutOrder(v.PetID.Value)
		end
	end
end

for i,v in pairs(Pets:GetChildren()) do
	for d,k in pairs(script.Parent.Inventory:GetChildren()) do
		if k.ClassName == "Frame" then
			if k.PetID.Value == v.PetID.Value then
			else
				newUI(v)
			end
		end
	end
end
Pets.ChildAdded:Connect(function(PetFolder)
	local Equipped = 0
	for i,v in pairs(Pets:GetChildren()) do
		if v:WaitForChild("Equipped").Value == true then
			Equipped = Equipped + 1
		end
	end	
	StorageText.Text = #Pets:GetChildren() .. "/" .. Data.MaxStorage.Value
	EquippedText.Text = Equipped .. "/" .. Data.MaxEquip.Value
end)

Pets.ChildRemoved:Connect(function(PetFolder)
	for i,v in pairs(InventoryFrame:GetChildren()) do
		if not v:IsA("UIGridLayout") then
			if v.PetID.Value == PetFolder.PetID.Value then
				v:Destroy()
			end
		end
	end
	local Equipped = 0
	for i,v in pairs(Pets:GetChildren()) do
		if v.Equipped.Value == true then
			Equipped = Equipped + 1
		end
	end	
	StorageText.Text = #Pets:GetChildren() .. "/" .. Data.MaxStorage.Value
	EquippedText.Text = Equipped .. "/" .. Data.MaxEquip.Value
end)

InventoryFrame.ChildAdded:Connect(function()
	InventoryFrame.CanvasSize = UDim2.new(0, 0, 0, InventoryFrame.UIGridLayout.AbsoluteContentSize.Y + 200)
end)

InventoryFrame.ChildRemoved:Connect(function()
	InventoryFrame.CanvasSize = UDim2.new(0, 0, 0, InventoryFrame.UIGridLayout.AbsoluteContentSize.Y + 200)
end)

-- Text Tools

local Equipped = 0

for i,v in pairs(Pets:GetChildren()) do
	if v.Equipped.Value == true then
		Equipped = Equipped + 1
	end
end


Data.MaxStorage:GetPropertyChangedSignal("Value"):Connect(function()
	StorageText.Text = #Pets:GetChildren() .. "/" .. Data.MaxStorage.Value
end)

Data.MaxEquip:GetPropertyChangedSignal("Value"):Connect(function()
	EquippedText.Text = Equipped .. "/" .. Data.MaxEquip.Value
end)

StorageText.Text = #Pets:GetChildren() .. "/" .. Data.MaxStorage.Value
EquippedText.Text = Equipped .. "/" .. Data.MaxEquip.Value

I thank you for every Help :))

1 Like

You need a world model and a camera in the viewport frame

1 Like

You don’t need a worldmodel unless you want to do raycasts or animations.

2 Likes

How can i do that? I never needed a camera

1 Like