Problem Moving & rotating models with :SetPrimaryPartCFrame

I built and placed 100’s of basic wooden fences, I then created variations of different fences for a script to decide randomly which ones shall get switched out and stay default.

The problem I’m having is placing and orienting said fences after they get cloned. They don’t change position as they should or orientation and the script throws no errors when I run it in the command bar.

any help would be appreciated, thanks in advance.

–said script


local Common = {"Broken_Top_Fence", "Wood_Fence_Bent_Top", "Wood_Fence_Broken_Top"}
local Uncommon = {"Broken_One_Side_Fence","Slanted_Fence"}
local Rare = {"Fence_Falling_Apart", "Collapsing_Fence"}



for a, b in ipairs(workspace:GetChildren()) do print(a.."/"..#workspace:GetChildren())

	if b.Name == ("Wood_Fence") then print("Got wood fence!")
		local decider = math.random(1,100)

		local rando_Fence = nil
		local x, y, z = b.WorldPivot.Rotation:ToOrientation()
		
		
		if decider <= 40 then
			rando_Fence = ("Wood_Fence")
		elseif decider <= 63 then
			rando_Fence = Common[math.random(1,#Common)]
		elseif decider <= 81 then
			rando_Fence = Uncommon[math.random(1,#Uncommon)]
		elseif decider <= 100 then
			rando_Fence = Rare[math.random(1,#Rare)]
		end
		
		print("Rarity = "..decider)

		local Fence_Piece = workspace:FindFirstChild(rando_Fence):Clone()

		Fence_Piece.Parent = workspace
		
		print("Parented "..Fence_Piece.Name)

		local Rot_Dec = math.random(1,2)

		
--[[where the error is]]
		if Rot_Dec == 1 then print("Reg rotation")
			b:SetPrimaryPartCFrame(CFrame.new(b.WorldPivot.Position.X,b.WorldPivot.Position.Y, b.WorldPivot.Position.Z ) * CFrame.Angles(math.rad(math.deg(x)),math.rad(math.deg(y)),math.rad(math.deg(z)))) 
		else print("Rotated_sideways")
			b:SetPrimaryPartCFrame(CFrame.new(b.WorldPivot.Position.X,b.WorldPivot.Position.Y, b.WorldPivot.Position.Z ) * CFrame.Angles(math.rad(math.deg(x)),math.rad(math.deg(y*-1)),math.rad(math.deg(z)))) 
		end
		--[[where the error ends]]

		b:Destroy()

	end


	wait()
end





1 Like

Why are you setting the CFrame.new to b.WorldPivot.Position, why not just Position?

What do you mean? Do they change in a way that is wrong, or do they not change at all? If they change in a way that’s wrong, please post a screenshot.

they just keep the position and ori of the clone

Position is not a valid member of Model “Workspace.Wood_Fence”

For a model, you must use model:PivotTo to move the model to a new position. It takes a CFrame, so if you are also doing orientation, you can do this:

local newCF = CFrame.new(position) * CFrame.Angles(
		math.rad(angleX), math.rad(angleY), math.rad(angleZ))
model:PivotTo(newCF)

You use CFrame.new() to for the position and CFrame.Angles() for the orientation of the model.

Model:PivotTo
CFrame.Angles

EDIT: Since you are REPLACING a model and keeping the same position and orientation, you can do this:

local cframe = oldModel:GetPivot()
newModel:PivotTo(cframe)

That way you don’t need to manipulate the CFrame directly.

2 Likes

Try using b:GetPivot().Position.X instead.
:SetPrimaryPartCFrame is also deprecated in favor of :GetPivot, so maybe use that instead.

Won’t work. model:GetPivot is a method, so your line should read b:GetPivot().Position.X instead of b:GetPivot.Position.X.

Was typing very quickly, my mistake.

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