How do I change the rotationa and the position of a model?

  1. What do you want to achieve?
    I want to make a simple building system.
  2. What is the issue?
    It works just fine but when I tried to make it so when you press r to rotate the part/model it doesnt do anything(no errors).

This is the script I am using and like I said when I press R it doesnt give any errors and it doesnt rotate but the position changing thing works tho.

--variables
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local runservice = game:GetService("RunService")
local uis = game:GetService("UserInputService")
local preview = workspace:WaitForChild("Model")
local Rotamount = 0
--checking for mouse position and setting preview part to its position
runservice.RenderStepped:Connect(function()
	mouse.TargetFilter = workspace:WaitForChild("Model")
	Cframepos = CFrame.new(mouse.Hit.Position.X, mouse.Hit.Position.Y + preview:WaitForChild("PPrimary").Size.Y/2, mouse.hit.Position.Z)
	Cframerot = CFrame.Angles(0,math.rad(Rotamount),0)
	preview:PivotTo(Cframepos, Cframerot)
end)
--cloning the preview part and changing the properties
uis.InputBegan:Connect(function(obj)
	if obj.UserInputType == Enum.UserInputType.MouseButton1 then
		print("Cloned")
		local clone = preview:Clone()
		clone:PivotTo(Cframepos, Cframerot)
		clone.Parent = workspace
		for i, v in pairs(clone:GetChildren()) do
			v.Material = "Wood"
			v.BrickColor = BrickColor.new("Brown")
			if v.Name ~= "PPrimary" then
				v.CanCollide = true
				v.Transparency = 0
			end
		end
	end
end)
--checking if r is pressed
uis.InputBegan:Connect(function(obj)
	if obj.KeyCode == Enum.KeyCode.R then
		print("R pressed")
		Rotamount = Rotamount + 90
		print(Rotamount)
	end
end)

PivotTo does not take two parameters. Try to multiply pos and rotation instead.

1 Like

It worked, thank you!
I really have to read the api reference about CFrames and how to do math with it :wink:

1 Like