Rotation Not Working as it Should

I recently followed a tutorial created by Egomoose where he showed people how to create a placement system. I’ve then modified it to be more like I imagine it to be like in my experience by switching the keyboard controls to visible buttons for compatibility with mobile. I got the place button to work but when I click the rotate button, the furniture rotates randomly. I’ll show you the rotate parts of the script and then record a video of it happening.

--ClientPlacement - LocalScript
local function onRotate(actionName, userInputState, input)
	rotation = rotation + math.pi/2
end

game:GetService("RunService").RenderStepped:Connect(function(dt)
	gui.Background.Rotate.MouseButton1Click:Connect(function()
		onRotate()
		local cf = placement:CalcPlacementCFrame(model, model.PrimaryPart.Position, rotation)
		model:SetPrimaryPartCFrame(cf)
	end)
end)

--------------------------------------------------------------------------------------------------

--Placement - ModuleScript - This one hasn't been edited but I'll put it in here so you can see it.
function Placement:CalcPlacementCFrame(model, position, rotation)
	local cf, size = self:CalcCanvas()

	local modelSize = CFrame.fromEulerAnglesYXZ(0, rotation, 0) * model.PrimaryPart.Size
	modelSize = Vector3.new(math.abs(modelSize.x), math.abs(modelSize.y), math.abs(modelSize.z))

	local lpos = cf:pointToObjectSpace(position);
	local size2 = (size - Vector2.new(modelSize.x, modelSize.z))/2
	local x = math.clamp(lpos.x, -size2.x, size2.x);
	local y = math.clamp(lpos.y, -size2.y, size2.y);
	
	local g = self.GridUnit
	if (g > 0) then
		x = math.sign(x)*((math.abs(x) - math.abs(x) % g) + (size2.x % g))
		y = math.sign(y)*((math.abs(y) - math.abs(y) % g) + (size2.y % g))
	end
	
	return cf * CFrame.new(x, y, -modelSize.y/2) * CFrame.Angles(-math.pi/2, rotation, 0)
end

Try removing renderstep, there is no need to continuously connect a connection.

Once connected it’ll listen until disconnected or script is destroyed.

Plus they can overlap.

1 Like

Ohh, that would make sense. I completely forgot that you wouldn’t need to call it again and again. Stupid mistake. :man_shrugging: