Trying to snap the blocks rotation to face the player?

So I am working on a placement system like Minecraft when when you place a block the front of it will face the player and at the moment I got a bit of it to work using CFrame.LookAt() but I can’t seem to get it to snap its rotation to a block and its rotation is all offsetted.

my script so far.

--Objects//
local PlacementEvent = game.ReplicatedStorage:FindFirstChild("PlacementEvent")
local BlocksFolder = game.ReplicatedStorage:FindFirstChild("Blocks")

--Functions//
PlacementEvent.OnServerEvent:Connect(function(plr, SelectedName, SelectedPosition, RootPosition)
	local Selected = BlocksFolder:FindFirstChild(SelectedName)
	if Selected then
		local NewBlock = Selected:Clone()
		NewBlock.Parent = workspace
		
		local NewCframe = CFrame.new(SelectedPosition, RootPosition)
		NewBlock:SetPrimaryPartCFrame(NewCframe)
	end
end)

If you would Like anymore information or screenshots then please ask :slight_smile:

You can use the :ToOrientation() property of the CFrame

Example:

local function Round(x,y)
	return math.round(x/y)*y
end
local Cframe = CFrame.Angles(math.pi,math.pi/2,math.pi/4)*CFrame.new(0,1,0)
local X,Y,Z = Cframe:ToOrientation()
local XDeg,YDeg,ZDeg = Round(math.deg(X),10),Round(math.deg(Y),10),Round(math.deg(Z),10)
local FinalCFrame = CFrame.Angles(math.rad(XDeg),math.rad(YDeg),math.rad(ZDeg))*CFrame.new(Cframe.Position)
1 Like

How would I apply this to my script?

local NewCframe = CFrame.new(SelectedPosition, RootPosition)
local snapsize = 10
local function Round(x,y)
	return math.round(x/y)*y
end
local X,Y,Z = NewCframe:ToOrientation()
local XDeg,YDeg,ZDeg = Round(math.deg(X),snapsize),Round(math.deg(Y),snapsize),Round(math.deg(Z),snapsize)
local FinalCFrame = CFrame.Angles(math.rad(XDeg),math.rad(YDeg),math.rad(ZDeg))*CFrame.new(SelectedPosition)
NewBlock:SetPrimaryPartCFrame(NewCframe)

That didnt work sorry it didnt change anything but when I tried setting its frame to the final frame it just make the blocks go all around the place and didn’t fix the rotation.

edit: nvm I manged to fix the rotation by setting the snapsize to 90 but now the only issue is the position not being where it should be.

local FinalCFrame = CFrame.Angles(math.rad(XDeg),math.rad(YDeg),math.rad(ZDeg))*CFrame.new(SelectedPosition)

with

local FinalCFrame = CFrame.new(SelectedPosition)*CFrame.Angles(math.rad(XDeg),math.rad(YDeg),math.rad(ZDeg))

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