Yes, that’s true. I usually leave how I did it at the end but I was in a bit of a rush this time around.
1 - Preparation
First, I give the model a PrimaryPart. I then give the model an IntValue which I call the “ZPosPreview” as it stores the Z value that we will use later to move the model forwards or backwards. I rotate the model how I want during this part. I then place the model in some container.
2 - GUI Set up
You obviously don’t need to follow my way of setting up the GUI but if you’re just here to test this out then this is how I set it up:
3 - Code
Then, we start coding it. This code is quite messy as I was just testing this out.
wait(.3)
local fram1 = script.Parent.Frame.ViewportFrame
local fram2 = script.Parent.Frame2.ViewportFrame -- 'fram2' is a second ViewportFrame as I was testing several ViewportFrames using one camera.
local camera = Instance.new("Camera") -- Creating the camera.
camera.Parent = script.Parent -- Giving the camera a humble abode.
fram1.CurrentCamera = camera -- Setting the CurrentCamera of the ViewportFrame(s) to our now cozy camera.
fram2.CurrentCamera = camera
camera.CFrame = CFrame.new(Vector3.new(0,0,0)) -- Setting the Camera's CFrame to 0,0,0.
------- This code will depend on how you store your model/part -------
local ItemStorage = game.ReplicatedStorage.ItemStorage -- This is where I store my models.
local Shields = ItemStorage:WaitForChild("Shields") -- This is specifically where I store my shield models.
local Weapons = ItemStorage:WaitForChild("Weapons") -- This is specifically where I store my weapon models.
local ItemArray1 = Shields:GetChildren() -- I get all of the Shields
local ItemArray2 = Weapons:GetChildren() -- ...and all of the Weapons
local item1 = ItemArray1[math.random(1, #ItemArray1)]:Clone() -- I pick a random shield.
local item2 = ItemArray2[math.random(1, #ItemArray2)]:Clone() -- I pick a random weapon.
-------------------------------------------------------------------------
local PCF1 = item1.PrimaryPart.CFrame - item1.PrimaryPart.CFrame.Position -- I preserve the rotation of the model in order to, well, preserve the rotation.
local PCF2 = item2.PrimaryPart.CFrame - item2.PrimaryPart.CFrame.Position
-- Here below, I set the PrimaryPartCFrame of the cloned Model to 0,0, and then the Z value is whatever the value of ZPosPreview is (the value that we made earlier). I then multiply it by the rotation that we got earlier to give it the original rotation.
item1:SetPrimaryPartCFrame(CFrame.new(Vector3.new(0,0,item1.ZPosPreview.Value)) * PCF1)
item2:SetPrimaryPartCFrame(CFrame.new(Vector3.new(0,0,item2.ZPosPreview.Value)) * PCF2)
item1.Parent = fram1 -- Then, I give the model a nice cozy place to stay.
item2.Parent = fram2
And then voilà! Perfectly preserved rotation.