How to make Placement system make the object rotate when click R?

Hello, I have this issue where i cant seem to find a answer to this.
I need my part to rotate when you need it to.

here is my script, parented to serverscriptservice.:

local placedObjects = workspace:WaitForChild("PlacedObjects")

local objects = RS:WaitForChild("Objects")


local function weld(currentObj)
	for objs, obj in pairs(currentObj:GetDescendants()) do
		if obj:IsA("BasePart") and obj ~= currentObj.PrimaryPart then
			local weldConstraint = Instance.new("WeldConstraint")

			weldConstraint.Part0 = obj
			weldConstraint.Part1 = currentObj.PrimaryPart

			weldConstraint.Parent = obj
			obj.Anchored = false
			obj.CanCollide = false
		end
	end
	
	currentObj.PrimaryPart.CanCollide = false

	return
end


local function init(obj)
	local hitbox = Instance.new("Part")

	local orientation, size = obj:GetBoundingBox()

	hitbox.Size = size
	hitbox.CFrame = orientation

	hitbox.Transparency = 1
	hitbox.Parent = obj
	hitbox.Anchored = true
	obj.PrimaryPart = hitbox

	weld(obj)

	return
end


local function place(obj, posX, posZ)
	local orientationY = obj.PrimaryPart.Orientation.Y
	
	obj:SetPrimaryPartCFrame(CFrame.new(posX, obj.PrimaryPart.Position.Y, posZ) * CFrame.Angles(0, math.rad(orientationY), 0))
	obj.PrimaryPart.CanCollide = true
	return
end


placeObject.OnServerEvent:Connect(function(plr, objectName, posX, posZ)
	local foundObject = objects:FindFirstChild(objectName)
	
	if foundObject then
		local clonedObject = foundObject:Clone()
		
		init(clonedObject)
		place(clonedObject, posX, posZ)
		
		clonedObject.Parent = placedObjects
	end
end)

Here is my local script. Parented to startercharacterscript.

ocal CS = game:GetService("CollectionService")
local TS = game:GetService("TweenService")
local RS = game:GetService("ReplicatedStorage")

local placeObject = RS:WaitForChild("PlaceObject")

local TI = TweenInfo.new(.4)

local objects = RS:WaitForChild("Objects")
local temporaryObjects = workspace:WaitForChild("TemporaryObjects")

local plr = game.Players.LocalPlayer

local mouse = plr:GetMouse()
local char = script.Parent


local currentObject = nil
local debounce = false



local function move() 
	local posX = math.floor(mouse.Hit.X)
	local posZ = math.floor(mouse.Hit.Z)
	local orientationY = currentObject.PrimaryPart.Orientation.Y
	
	debounce = true
	
	local Tween = TS:Create(currentObject.PrimaryPart, TI,
		{CFrame = CFrame.new(posX, currentObject.PrimaryPart.Position.Y, posZ) * CFrame.Angles(0, math.rad(orientationY), 0)})
	Tween:Play()
	Tween.Completed:Wait()
	
	debounce = false
end


local function weld()
	debounce = true
	
	for objs, obj in pairs(currentObject:GetDescendants()) do
		if obj:IsA("BasePart") and obj ~= currentObject.PrimaryPart then
			local weldConstraint = Instance.new("WeldConstraint")
			
			weldConstraint.Part0 = obj
			weldConstraint.Part1 = currentObject.PrimaryPart
			
			weldConstraint.Parent = obj
			obj.Anchored = false
			obj.CanCollide = false
		end
	end
	
	mouse.TargetFilter = currentObject
	
	debounce = false
	
	currentObject.PrimaryPart.CanCollide = false
	
	return
end


local function init()
	local hitbox = Instance.new("Part")
	
	local orientation, size = currentObject:GetBoundingBox()
	
	hitbox.Size = size
	hitbox.CFrame = orientation
	
	hitbox.Transparency = 1
	hitbox.Parent = currentObject
	hitbox.Anchored = true
	currentObject.PrimaryPart = hitbox
	
	weld()
	
	return
end


local function place(tool)
	placeObject:FireServer(currentObject.Name, currentObject.PrimaryPart.Position.X, currentObject.PrimaryPart.Position.Z)
	
	temporaryObjects:ClearAllChildren()
	tool:Destroy()
end



local function checkForEquipped()
	for index, tool in pairs(CS:GetTagged("PlacementObject")) do
		local moveConnection = nil
		
		tool.Equipped:Connect(function()
			local foundObject = objects:FindFirstChild(tool.Name)
			
			if foundObject then
				currentObject = foundObject:Clone()
				
				currentObject.Parent = temporaryObjects
				
				init()
				
				moveConnection = mouse.Move:Connect(move)
				move()
				
				tool.Activated:Connect(function()
					if moveConnection ~= nil then
						moveConnection:Disconnect()
						
						moveConnection = nil
						
						place(tool)
					end
				end)
			end
		end)
		
		tool.Unequipped:Connect(function()
			if moveConnection ~= nil then
				moveConnection:Disconnect()

				moveConnection = nil
				
				temporaryObjects:ClearAllChildren()
			end
		end)
	end
end


checkForEquipped()

If you know how to fix, please leave a comment!

local rotation = 0

local function onRotate(actionName, userInputState, input)
	if (userInputState == Enum.UserInputState.Begin) then
		rotation = rotation + math.pi/4
	end
end

game:GetService("ContextActionService"):BindAction("rotate", onRotate, false, Enum.KeyCode.R)

Hope that helped. Let me know of any issues.

1 Like

can you import it into my code? im pretty new to coding and my knowledge is limited.

I think you can do like this, have this somewhere in a while wait() true loop or something similar so that it is always updating.

model:SetPrimaryPartCFrame(model.PrimaryPart.CFrame * CFrame.Angles(0, rotation, 0))

Cant find the solution tho, i put it in a while true loop and it wont work, may you insert the code into my script for me?