How can I save this old CFrame position and add it to this new one so I can make this rotating thing?

heres my code
target:SetPrimaryPartCFrame(CFrame.Angles(0, math.rad(mouse.X / 5), 0) * CFrame.Angles(math.rad(-mouse.Y / 3), 0, 0))
this will rotate the model depending on the mouse position but I want after you stop rotating the item it will keep that same position and then you can begin rotating from that position. If that makes sense.

1 Like

Can you explain in more detail? The code you posted doesn’t really accomplish anything that makes sense. The part just rotates randomly as you move your mouse.

Couldn’t you put a variable for the position to save it.

Heres a video:

see the problem is I want to keep rotating the item but it only rotates based on the position of the mouse. So for example If I wanted to rotate it 360 degrees I cant because the mouse doesnt have enough room on the screen.

1 Like

Ive tried but I dont know how to add it to it.

After the rotating code, you can add a variable like:

local newcframe = --pathtoobject-- .CFrame

I mean i made a variable holding the last position so after its done rotating it will get the last position but I dont know how to add it to the rotating code.

Instead of using mouse.X use mouse delta to drag the object.

https://developer.roblox.com/en-us/api-reference/function/UserInputService/GetMouseDelta

Also it’s been done before same concept but with a different method of finding mouse delta.

Alright, sorry for the late response but I threw together some rather ugly code but it’s late.

https://gyazo.com/0860df478f7b1d4bd2f84c216c93f192

local cf = workspace.Part2.CFrame * CFrame.new(0, 0, 5)
local orientationZ = math.atan2(workspace.Part2.CFrame.LookVector.X, workspace.Part2.CFrame.LookVector.Y) 

game:GetService('UserInputService').InputChanged:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseMovement then
		workspace.CurrentCamera.CameraType = Enum.CameraType.Scriptable
		workspace.CurrentCamera.CFrame = cf

		local scale = Vector2.new(input.Position.X, input.Position.Y)/workspace.CurrentCamera.ViewportSize + Vector2.new(0.5, 0)
		workspace.Part2.CFrame = CFrame.new(workspace.Part2.Position) * CFrame.fromEulerAnglesYXZ(scale.Y * math.pi * 2, scale.X * math.pi * 2, orientationZ)
	end
end)

You can ignore the camera cframing, but basically what it does is gets the mouse X and Y and divides it by total screen size (basically normalizing the position, aka (0, 0) means mouse is at top left and (1, 1) means it’s at the bottom right), then subtracting 0.5 from the X (so the middle of the screen = 0) then multiplying it by pi * 2 (which is just 360 degrees). It then applies the angles to the part in YXZ order. The orientationZ is there to preserve the Z orientation of the part.

so im assuming this is what I paste in?? But replace it with my object etc.

So I got everything working by using that guys example and I fixed his because he had a couple mistakes heres the fully fledged working code except for one problem

local function inspect_Rotate(rotationCheck, LastMousePos)
	runService.RenderStepped:Connect(function()
		if ViewportFrame.CurrentCamera ~= nil and rotationCheck then
			local target = ViewportFrame:WaitForChild("Model")

			--target:SetPrimaryPartCFrame(CFrame.Angles(0, math.rad(mouse.X / 5), 0) * CFrame.Angles(math.rad(-mouse.Y / 3), 0, 0))

			playerGui.FakeMouse.Enabled = false
			local CurrentMousePos = Vector2.new(mouse.X,mouse.Y)
			local change = (CurrentMousePos - LastMousePos)/5
			local target = ViewportFrame:FindFirstChild("Model")

			target:SetPrimaryPartCFrame(target:GetPrimaryPartCFrame() * CFrame.Angles(0, math.rad(change.X), 0) * CFrame.Angles(math.rad(change.Y), 0, 0))

			LastMousePos = CurrentMousePos

			target.PrimaryPart.Orientation = Vector3.new(0, 0, 0)

			return target, LastMousePos
		else
			playerGui.FakeMouse.Enabled = true
		end
	end)
end

when running the if ViewportFrame.CurrentCamera ~= nil and rotationCheck then the rotationCheck is supposed to stop the code from running after the player has released space. But for some reason it still runs heres my userInputService code for when the player presses and holds space;

if input.KeyCode == Enum.KeyCode.Space and beingInspected and not gameProccessedEvent then
	LastMousePos = Vector2.new(mouse.X,mouse.Y)
	rotationCheck = true
	inspect_Rotate(rotationCheck, LastMousePos)
	return rotationCheck
end

heres for when the player stops pressing and holding space;

userInputService.InputEnded:Connect(function(input, gameProccessedEvent)
	if input.KeyCode == Enum.KeyCode.Space and not gameProccessedEvent then
		LastMousePos = nil
		rotationCheck = false
		inspect_Rotate(rotationCheck, LastMousePos)
		return rotationCheck
	end
end)

and what happens is I ran a print on the runService loop it will print true when holding the space but as soon as I let go it will then spam true and it will also spam false but I dont know why any ideas anyone??

It’s somewhat difficult to understand what you want, but I’ll give it my best shot. What I think you want is this: To be able to rotate model while it remains in place. All you need to do this is multiple the target’s CFrame by the rotational CFrame. Also, Model | Roblox Creator Documentation is deprecated. Use PVInstance | Roblox Creator Documentation instead. So to rotate it use something like this:

-- target:PivotTo(target:GetPivot() * CFrame.Angles(0, 0, math.rad(5)))

target:PivotTo(target:GetPivot() * CFrame.Angles(0, math.rad(mouse.X / 5), 0) * CFrame.Angles(math.rad(-mouse.Y / 3), 0, 0))

You can use the InputObject itself for this, instead of monitoring every input that ends. This would look like this:

userInputService.InputBegan:Connect(function(input, gameProccessedEvent) -- you only have to monitor input that begins
	if input.KeyCode == Enum.KeyCode.Space and not gameProccessedEvent then
		
		-- keeps track of the 'input' object and detects when it ends
		local end
		end = input.Changed:Connect(function()
			if input.UserInputState == Enum.UserInputState.End then -- if input ended
				end:Disconnect() -- disconnect the event

				LastMousePos = nil
				rotationCheck = false
				inspect_Rotate(rotationCheck, LastMousePos)
				return rotationCheck
			end
		end)

		-- Other code
	end
end)

Alright I got it working but the problem still persists. Heres my code;

userInputService.InputBegan:Connect(function(input, gameProccessedEvent)
	if input.KeyCode == Enum.KeyCode.Space and not gameProccessedEvent and beingInspected then


		local stopped
		stopped = input.Changed:Connect(function()
			if input.UserInputState == Enum.UserInputState.End then -- if input ended
			print("1")
			stopped:Disconnect() -- disconnect the event

			LastMousePos = nil
			rotationCheck = false
			inspect_Rotate(rotationCheck, LastMousePos)
			return rotationCheck
			end
		end)

		LastMousePos = Vector2.new(mouse.X,mouse.Y)
		rotationCheck = true
		inspect_Rotate(rotationCheck, LastMousePos)
		return rotationCheck
	end
end)

Now ill send a video in it you’ll see when it prints 1 thats the code detecting that I have stopped pressing space but the item keeps rotating;

1 Like

Sorry about that. I didn’t provide you with the complete solution I used in a similar scenario because I didn’t think you would need it. I basically set it up so that nothing was outside the control of the input ended section of code I provided. That way it was all contained. My setup was like this:

local stopped
stopped = {
	ended = input.Changed:Connect(function()
		if input.UserInputState == Enum.UserInputState.End then
			stopped.changed:Disconnect()
			stopped.ended:Disconnect()
			stopped.changed = nil
			stopped.ended = nil
			stopped = nil

			-- other additional code that is dependent on what you need reset after it's ended
		end
	end),

	changed = userInputService.InputChanged:Connect(function(input)
		
		-- your code for processing the user's mouse movements. this way when they stop this function is disconnected as well
	end),
}

Its actually okay I fixed it! I figured that out as well as the CFrame equation and a looping problem I had if you want to play it and test it I can pm you the link I just need to fix up some UI stuff and make an inventory system thanks.

np. I wouldn’t mind you pming the link to me.