Objects inside model are slightly off their position after changing model position in script

I want to make a viewportframe of each pet.

So there are some pets inside a folder. Im looping through the a table that has all these pets. The obj variable is the model itself. The bodyObj variable is the PrimaryPart and the main part of pet (as in like the body). There are other objects inside the model like black outlines for the pets or ears.

What I’m trying to do is weld these objects to the body object, so when i change the position of the body object to the position of the viewportframe, they will follow it.

My problem is that when I do obj:PivotTo(cframe) or obj:SetPrimaryPartCFrame(cframe), they follow the body object but they are slightly offset. I am 100% sure that the object before is perfect, just after it running through the script and getting into the viewportframe, the ears or black outline arent perfect anymore.
I’ve spent alot of time trying to find a solution but nothing worked.

local viewportFrameCframe = rep.InventorySystem["ViewportFrame Tests"].Pets.PositionForPetViewportFrameCFRAME.Value
	local bodyObj = obj:FindFirstChild("Body")
	if bodyObj ~= nil then
		obj.PrimaryPart = bodyObj
	end
	for z, y in pairs(obj:GetChildren()) do
		if y.Name ~= "Body" and bodyObj ~= nil then --the bodyObj's Name is "Body"
			local weldCons1 = Instance.new("WeldConstraint")
			weldCons1.Name = "WeldCons"
			weldCons1.Part0 = bodyObj
			weldCons1.Part1 = y
			weldCons1.Parent = y
			y.Orientation = Vector3.new(0, -90, 0)
		end
	end
	if bodyObj ~= nil then
		obj:PivotTo(viewportFrameCframe)
		bodyObj.Orientation = Vector3.new(0, -90, 0)
	end

image
image

2 Likes

Something that potentially be an issue is the pivot point used by the PivotTo() method. You can ensure that the objects are aligned correctly, and setting a custom pivot point using the SetPivot() method.

My Approach

local viewportFrameCframe = rep.InventorySystem["ViewportFrame Tests"].Pets.PositionForPetViewportFrameCFRAME.Value
local bodyObj = obj:FindFirstChild("Body")
if bodyObj ~= nil then
    obj.PrimaryPart = bodyObj
end
for z, y in pairs(obj:GetChildren()) do
    if y.Name ~= "Body" and bodyObj ~= nil then --the bodyObj's Name is "Body"
        local weldCons1 = Instance.new("WeldConstraint")
        weldCons1.Name = "WeldCons"
        weldCons1.Part0 = bodyObj
        weldCons1.Part1 = y
        weldCons1.Parent = y
        y.Orientation = Vector3.new(0, -90, 0)
    end
end
if bodyObj ~= nil then
    -- Calculate the offset between the body object and the viewport frame
    local offset = viewportFrameCframe:ToObjectSpace(bodyObj.CFrame).Position
    -- Set the pivot point to the position of the body object
    obj:SetPivot(bodyObj.Position + offset)
    -- Move the object to the viewport frame
    obj:SetPrimaryPartCFrame(viewportFrameCframe)
    bodyObj.Orientation = Vector3.new(0, -90, 0)
end

→ Calculate the offset between the body obj and viewport frame
→ Set the pivot point of the obj to the pos of the body obj
→ Move the object to the viewport using SetPrimaryPartCFrame()

Hey, thanks for the help. I tried doing what you tried and it didn’t work. The SetPivot() method wasn’t working it was calling an error.

Either way the way i resolved this issue is that instead of changing the pets position to the viewportframe i just made a new camera instance and changed that cameras cframe to the object and then made it the current camera of the viewportframe. This way i didnt change the pets position and nothing got shifted!

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