Need help with :PivotTo

Hi developers,

I need help with my script (just one part of it), because I’m still pretty new to this :angst:

When I make a copy from RS and use PivotTo to place it on a player’s fence, if it’s larger than 4 studs (in my case), it sinks right into the ground, and I don’t know what to do about it

Photo:


My part of the script:

	local plot = findPlayerPlot(player)
	if plot and not plot:FindFirstChild('fence') then
		local prop = plot:FindFirstChild('properties')
		if prop then
			local skinVariable = prop:FindFirstChild('skin')
			if skinVariable then
				local skinValue = skinVariable.Value
				
				--{custom rotate variable}--
				local rotation = 90
				
				--{if player plot has one of these names, then rotate it -90 degrees}--
				if plot.Name == 'plot_1' or plot.Name == 'plot_2' or plot.Name == 'plot_3' then
					rotation = -rotation
				end
				
				--// Change skin
				--{if new value is different than old one, then destroy old fence and copy new one}--
				skinVariable:GetPropertyChangedSignal('Value'):Connect(function()
					local newValue = skinVariable.Value
					if allSkins[newValue] then
						--{if fence is already in plot, then destroy it}--
						if plot:FindFirstChild('fence') then
							plot.fence:Destroy()
							
							task.wait(.1)
							local copy = allSkins[newValue]:Clone()
							copy.Parent = plot
							copy.Name = 'fence'
							copy:PivotTo(plot.fencePoint.CFrame * CFrame.Angles(0, math.rad(90), math.rad(rotation)))
						end
					end
				end)
				
				--{copy fence in replicated storage and then paste it into the plot}--
				if allSkins[skinValue] then
					local copy = allSkins[skinValue]:Clone()
					copy.Parent = plot
					copy.Name = 'fence'
					--{set the right position of the fence}--
					copy:PivotTo(plot.fencePoint.CFrame * CFrame.Angles(0, math.rad(90), math.rad(rotation)))
				end
			end
		end
	end
end

Thanks! :wink:

:PivotTo() pivots center of the model (if you dont use any PrimaryPart and you were not messing around with your pivot of model) to CFrame while I am sure you want to pivot bottom point of your model. You can use :GetExtentsSize() that returns size of the box that your model can fit in, this way you will know height of the model box. Then just add Vector3.yAxis * boxSize.Y / 2 to point you were about to use in :PivotTo().

1 Like

Another thing you can do is manually change pivot of your fences using Studio tool. Just make it a bottom point of each model.

1 Like

Make sure you have a primary part set for the fence model. So that the primary part sits at the exact point of the fencePoint. If it still goes in the ground you can always apply an offset by multiplying the CFrames by CFrame.new(0, 1, 0)

1 Like

This one, with the primary part worked perfectly for me! This is the first time I’ve worked with a pivot and things like that…
Thanks!

1 Like

Yep.. just remember the primary part is like where the whole model will be “connected to”. So if you have a big 100x100 cube… and place the primary part at the BOTTOM of the cube. If you use PivotTo() on the model, then it will pivot around the bottom of the big cube.

1 Like

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