You asked: How do I make this rotate on key down R?
Firstly, we must use the UserInputService which can detect user input via the keyboard.
1 | local UIS = game:GetService( "UserInputService" ); |
3 | UIS.InputBegan:Connect( function (key, gpe) |
5 | if (key.KeyCode = = Enum.KeyCode.R) then |
6 | NewTurret.CFrame = NewTurret.CFrame * CFrame.Angles( 0 ,math.rad( 90 ), 0 ); |
This will rotate the turret 90 degrees on the Y axis. Always use CFrame.Angles() when dealing with rotation. In my example I'm assuming NewTurret is a part, or an object that has a CFrame. If it's a model you could use
1 | local UIS = game:GetService( "UserInputService" ); |
3 | UIS.InputBegan:Connect( function (key, gpe) |
5 | if (key.KeyCode = = Enum.KeyCode.R) then |
6 | NewTurret:SetPrimaryPartCFrame(NewTurret.PrimaryPart.CFrame * CFrame.Angles( 0 ,math.rad( 90 ), 0 )); |
Although when using the provided code above you must have the primary part set already on the model. math.rad() takes an argument in degrees and converts it to radians which is what CFrame.Angles() uses.