Rotating a part when A/D key gets pressed

Hello,

So I already have the A/D key pressing script but I have no clue how I can rotate the part any help would be appreciated!

angle = 5 --your angle
part.CFrame = part.CFrame * CFrame.Angles(math.rad(angle), 0, 0)

also try different axises

I used that code but the problem is that when I hold the key it only changes it one time and it needs to change it the entire time the key is pressed.

make a variable and a while loop

angle = 5 --your angle
local keydown = false --if key is down

spawn(function()
     while true do
          if keydown == true then
               --rotation stuff
               part.CFrame = part.CFrame * CFrame.Angles(math.rad(angle), 0, 0)
          end
          wait() --delay
     end
end)

make it so that when the key is pressed, keydown is set to true (do this for both keys)

Use CFrame, also if you post your code, I might be able to help a bit more.

If the Part has to be Anchored use CFraming as mentioned above.
You can also use a HingeConstraint with it set to ‘motor’ and make the A/D keys change the rotation speed to + or -.

@Scottifly what do I need to attach the HingeConstraint cause its just a part.

A HingeConstraint has 2 Attachments, one in the Part that’s rotating and one on another Part. The other Part can be Anchored or Unanchored. You can change the Position of the HingeConstraint to anywhere, which means you can even put it in the Baseplate, just offset to where you want the Hinge to be.
That’s how wheels on Vehicles work as well as spinning Platforms.

1 Like

@Scottifly My part is unanchored and the BasePlate is anchored but when I press E the part doesn’t goes up anymore and it also doesn’t rotates. Does this has to do with what I just said above?

local bodyPos = Instance.new("BodyPosition")
bodyPos.Parent = game.Workspace.Part

local inputService = game:GetService("UserInputService")

local vel = game.Workspace.Part.BodyVelocity
local bg = game.Workspace.Part.BodyGyro
local pressed = false

local up = 0
local upSpeed = 5

local part = game.Workspace.Part

local rotateDirection = game.Workspace.Baseplate.HingeConstraint.AngularVelocity

inputService.InputBegan:Connect(function(input, gameProcessedEvent)
	if input.UserInputType == Enum.UserInputType.Keyboard then
		if input.KeyCode == Enum.KeyCode.E then
			
			pressed = true
			vel.Velocity = Vector3.new(0,5,0)
			
			
		end
	end
end)

inputService.InputEnded:Connect(function(input, gameProcessedEvent)
	if input.UserInputType == Enum.UserInputType.Keyboard then
		pressed = false
		if pressed == false then
			vel.Velocity = Vector3.new(0,0,0)
		end
	end
end)

inputService.InputBegan:Connect(function(input, gameProcessedEvent)
	if input.UserInputType == Enum.UserInputType.Keyboard then
		if input.KeyCode == Enum.KeyCode.Q then
			
			pressed = true
			vel.Velocity = Vector3.new(0,-5,0)
		end
	end
end)

inputService.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.Keyboard then
		if pressed == false then
			vel.Velocity = Vector3.new(0,0,0)
		end
	end
end)

inputService.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.Keyboard then
		if input.KeyCode == Enum.KeyCode.F then
			pressed = true
			
			
			rotateDirection = -5
			
		end
	end
end)

inputService.InputEnded:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.Keyboard then
		pressed = false
		
		rotateDirection = 0
	end
end)

inputService.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.Keyboard then
		
		if input.KeyCode == Enum.KeyCode.R then
			pressed = true
			
			rotateDirection = 5
		end
	end
end)

inputService.InputEnded:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.Keyboard then
		pressed = false
		
		rotateDirection = 0
	end
end)

Have you set the HingeConstraint.MotorMaxTorque value of the HingeConstraint in its Properties? If not then try something like 100,000 or so. If the items are big or very dense then you may have to increase it more.

Also I’ve noticed that you can’t always reference the AngularVelocity directly in the variable.
local rotateDirection = game.Workspace.Baseplate.HingeConstraint.AngularVelocity
may need to be:
local rotateDirection = game.Workspace.Baseplate.HingeConstraint
and
rotateDirection = -5
may need to be:
rotateDirection.AngularVelocity = -5

@Scottifly It can rotate now but the part doesnt goes up.

Do you mean up and down, or just above the surface at a certain height?
If you just want it to spin in place go to your Model Tab up top and click the Show Constraints box and the Draw On Top box.
This will show you where your Attachments are located. You can move them to wherever you want. Just make sure the Orientation of the Attachments are the same. You will see them as the yellow and orange arrows that show up when you select the Attachment.
When you’re in Studio make sure the Attachment for the spinning Part is at located at 0,0,0 in the Part, and the Attachment in the Baseplate is at the same Position as the Part’s Attachment. I usually just go to the Attachment Properties, select and copy the world Position of the first attachment the paste that into the world Position of the second Attachment.

@Scottifly Yes I mean up and down but the part is free to move everywhere it wants so it can up/down/forward/backward/left/right.

And it still can’t go up. Even though I placed the Attachment in at 0,0,0 like you said. I’ve also set the position of the other Attachment that is attached to the BasePlate.

Also the part that makes it go up is BodyVelocity.

Jeez…
That information was very important. You should have mentioned that in your first post since you just said you wanted it to rotate when A and D are pressed and I’ve only given you information to rotate a Part in one Position.

If you are using BodyVelocity to move the Part then rotate it with a BodyGyro or a Torque Constraint.

@Scottifly Yes, I tried doing that but when I pressed A or D then it does rotates but if I press one of them again it rotates faster and faster. And if I first press A and then D it doesn’t goes smoothly.

I can’t test right now sadly but maybe try this-

local uis = game:GetService("UserInputService")
local rs = game:GetService("RunService")
local rotSpeed = 0

uis.InputBegan:Connect(function(input,gpe)
    if gpe then return; end

    if input.KeyCode == Enum.KeyCode.A then
        rotSpeed = -2 --// If -5 makes it go wrong way just change it
    end
end)

uis.InputBegan:Connect(function(input,gpe)
    if gpe then return; end

    if input.KeyCode == Enum.KeyCode.D then
        rotSpeed = 2 --// If it goes wrong way change this number
    end
end)

uis.InputBegan:Connect(function(input,gpe)
    if gpe then return; end

    if input.KeyCode == Enum.KeyCode.A or input.KeyCode == Enum.KeyCode.D then
        rotSpeed = 0
    end
end)

rs.RenderStepped:Connect(function()
    workspace["PartNameHere"].Orientation += rotSpeed --// If it rotates too fast add a /10 or /100 btw
end)

@FreeFlyHaker56

I’m getting that eror if I do that.