Item not appearing in viewport

local ReplicatedStorage = game:GetService('ReplicatedStorage')

local Weapons = ReplicatedStorage:WaitForChild('Weapons')

return function(viewport, item, itemType)
	local StoredItem = Weapons[itemType .. 's'][item].Handle
	local ClonedItem = StoredItem:Clone()
		
	local Camera = Instance.new('Camera')
	Camera.FieldOfView = 60
	
	ClonedItem.Parent = viewport
	
	ClonedItem.CFrame = CFrame.new(0, 0, 0)
			
	viewport.CurrentCamera = Camera
end

No errors, and items definately being cloned and what not. But it does not show in the viewport

Probably an issue with the Camera. IDK if it has to be parented to anything, so you could try that. Also, the default CFrame value is an identity matrix, meaning your camera is inside the item you want to preview. Just offset it in a direction.

Camera doesn’t have to be parented to the viewport, just the ViewportFrame.CurrentCamera has to be the camera.

I’ve tried all 6 axis (X, -X, Y, -Y, Z, -Z) only by 1 each time and it’s never appeared

Try this:
ClonedItem.CFrame = Camera.CFrame+Camera.CFrame.LookVector*10

Ok that seemed to work. How can I make them closer tho??

You can just replace the *10 with a smaller number. You can go even further and assign a different multiplier for each item so that larger items scale accordingly.

2 Likes

Hmm ok, and is there a way to rotate specific items? like the katana on the left looks good, but the shuriken needs to be rotated so it’s visible

if itemType == 'Katana' then
		ClonedItem.CFrame = Camera.CFrame + Camera.CFrame.LookVector * 5 
	else
		ClonedItem.CFrame = Camera.CFrame + Camera.CFrame.LookVector * 4 * CFrame.Angles(0, 0, 0)
	end

1 Like

Try doing ClonedItem.CFrame = (Camera.CFrame + Camera.CFrame.LookVector * 4) * CFrame.Angles(math.pi/2, 0, 0)
this will rotate the item 90 degrees on the X axis

You have to save it as a table for each item. I’d suggest storing it as a separate module with the item names as indices. You can also store some extra stuff in it like the name to display. You’ll have to mess around with the Orientation property in here until you get the desired result. Note that the orientation is done in radians, not degrees.

--ModuleScript "ItemData" parented to the script the code you posted is located in
return {
	Katana = {
		DisplayName="Katana",
		ViewportDistance = 7,
		ViewportOrientation = {0,0,0}
	},
	Shuriken = {
		DisplayName="Shuriken",
		ViewportDistance = 6,
		ViewportOrientation = {math.rad(90),0,0}
	}
}

Now to implement this into your current script:

local ReplicatedStorage = game:GetService('ReplicatedStorage')

local Weapons = ReplicatedStorage:WaitForChild('Weapons')
local ItemData = require(script:WaitForChild("ItemData"))

return function(viewport, item, itemType)
	local Data = ItemData[itemType]
	local StoredItem = Weapons[itemType .. 's'][item].Handle
	local ClonedItem = StoredItem:Clone()
		
	local Camera = Instance.new('Camera')
	Camera.FieldOfView = 60
	
	ClonedItem.Parent = viewport
	
	--The "or" part is to avoid errors if data of an item is icomplete
	ClonedItem.CFrame = CFrame.Angles(unpack(Data.ViewportOrientation or {0,0,0})) + Camera.CFrame.LookVector*(Data.ViewportDistance or 10)
			
	viewport.CurrentCamera = Camera
end