How can I assign player's camera style to a block

Hello, reader!

I am currently working on a build-mode camera (actually just to learn stuff, not for a game). I made the camera block move but when it came to changing its orientation to the mouse, I got stuck. I tried hard on experimenting with many things.

My script:

local runService = game:GetService("RunService")
local userInputService = game:GetService("UserInputService")
local replicatedStorage = game:GetService("ReplicatedStorage")

local changeHumanoidProperties = replicatedStorage.changeHumanoidProperties

local player = game.Players.LocalPlayer
local character = player.Character
local HRP = character:WaitForChild("HumanoidRootPart")
local humanoid = character:WaitForChild("Humanoid")
local defaultWalkSpeed = humanoid.WalkSpeed
local defaultJumpPower = humanoid.JumpPower

local mouse = player:GetMouse()

local camera = workspace.CurrentCamera
local buildCam = workspace.BuildCam

local isInCamera = false
local toggleButton = false
local isRightClick = false
local wKey = false
local aKey = false
local sKey = false
local dKey = false
local qKey = false
local eKey = false

script.Parent.MouseButton1Click:Connect(function()
	if not toggleButton then
		isInCamera = true
		script.Parent.Text = "Exit Build Mode"
		changeHumanoidProperties:FireServer("WalkSpeed", 0)
		changeHumanoidProperties:FireServer("JumpPower", 0)
		toggleButton = true
	else
		isInCamera = false
		script.Parent.Text = "Enter Build Mode"
		changeHumanoidProperties:FireServer("WalkSpeed", defaultWalkSpeed)
		changeHumanoidProperties:FireServer("JumpPower", defaultJumpPower)
		toggleButton = false
	end
end)

userInputService.InputBegan:Connect(function(input, isProcessed)
	if isProcessed then return end
	
	if isInCamera then
		if input.KeyCode == Enum.KeyCode.W then
			wKey = true
		elseif input.KeyCode == Enum.KeyCode.A then
			aKey = true
		elseif input.KeyCode == Enum.KeyCode.S then
			sKey = true
		elseif input.KeyCode == Enum.KeyCode.D then
			dKey = true
		elseif input.KeyCode == Enum.KeyCode.Q then
			qKey = true
		elseif input.KeyCode == Enum.KeyCode.E then
			eKey = true
		end
	end
end)

userInputService.InputEnded:Connect(function(input, isProcessed)
	if isProcessed then return end
	
	if input.KeyCode == Enum.KeyCode.W then
		wKey = false
	elseif input.KeyCode == Enum.KeyCode.A then
		aKey = false
	elseif input.KeyCode == Enum.KeyCode.S then
		sKey = false
	elseif input.KeyCode == Enum.KeyCode.D then
		dKey = false
	elseif input.KeyCode == Enum.KeyCode.Q then
		qKey = false
	elseif input.KeyCode == Enum.KeyCode.E then
		eKey = false
	end
end)

runService.RenderStepped:Connect(function()
	if isInCamera then
		camera.CFrame = buildCam.CFrame
	end
	if wKey then
		buildCam.Position = buildCam.Position + Vector3.new(0,0,-0.1)
	end
	if aKey then
		buildCam.Position = buildCam.Position + Vector3.new(-0.1,0,0)
	end
	if sKey then
		buildCam.Position = buildCam.Position + Vector3.new(0,0,0.1)
	end
	if dKey then
		buildCam.Position = buildCam.Position + Vector3.new(0.1,0,0)
	end
	if qKey then
		buildCam.Position = buildCam.Position + Vector3.new(0,-0.1,0)
	end
	if eKey then
		buildCam.Position = buildCam.Position + Vector3.new(0,0.1,0)
	end
end)

I deleted the camera move part from the script because it wasn’t working. How can I make it like the block’s orientation changes to where the mouse is looking at? I would love to hear an explanation.

Thanks in advance,
-pranvexploder. :smiley:

P.S. I already read this topic, but it came to no use for me.@goldenstein64’s reply worked, but that style couldn’t be assigned to a part.

Could you be more specific what you mean? Are you referring to where if you hold down right-click and move your mouse your camera will move accordingly? Like it does in studio?

If so, heres the solution I used (from the post you linked)

UserInputService.InputChanged:connect(function(inputObj)
	if inputObj.UserInputType == Enum.UserInputType.MouseMovement then
		local dx = inputObj.Delta.X / 2
		local dy = inputObj.Delta.Y / 2
		if (Camera.Orientation.X + -dy) <= MaxDirectionalView and (Camera.Orientation.X + -dy) >= -MaxDirectionalView then
			Camera.Orientation = Camera.Orientation + Vector3.new(-dy, -dx, 0)
		elseif (Camera.Orientation.X + -dy) <= MaxDirectionalView then
			Camera.Orientation = Vector3.new(-MaxDirectionalView, Camera.Orientation.Y, 0)
		elseif(Camera.Orientation.X + -dy) >= -MaxDirectionalView then
			Camera.Orientation = Vector3.new(MaxDirectionalView, Camera.Orientation.Y, 0)	
		end
	end
end)

It is the once holding right-click and moving across. Also, it should not move upwards, just a slight it. Another thing; can you explain me the code?

The script works perfectly fine after a bit of edits, marking it as solved. Thank you!

Also a new issue has popped up with this code:

runService.RenderStepped:Connect(function()
	if isInCamera then
		camera.CFrame = buildCam.CFrame
	end
	if wKey then
		buildCam.Position = buildCam.Position + Vector3.new(0,0,-0.1)
	end
	if aKey then
		buildCam.Position = buildCam.Position + Vector3.new(-0.1,0,0)
	end
	if sKey then
		buildCam.Position = buildCam.Position + Vector3.new(0,0,0.1)
	end
	if dKey then
		buildCam.Position = buildCam.Position + Vector3.new(0.1,0,0)
	end
	if qKey then
		buildCam.Position = buildCam.Position + Vector3.new(0,-0.1,0)
	end
	if eKey then
		buildCam.Position = buildCam.Position + Vector3.new(0,0.1,0)
	end
end)

I don’t think I need to explain the problem. It is the problem with moving the part via Vector3. I know I need to use LookVector for that, but it is behaving weird after I am using the CFrame.LookVector thing. Could you help me with it?

Could you go in depth with what your problem is? For moving in a certain direction I’d just use CFrames to make your life easier.

Here’s an example of just one,

buildCam.CFrame = buildCam.CFrame * CFrame.new(0,0,-0.1)

Well, I’m sorry for not making myself clear, but that’s the solution for my next problem. However, I encountered with another problem, how can I even limit the Y orientation instead of just making it rotate 360 degrees? I tried copying the if condition that you used for limiting the X orientation, but it just didn’t work.