Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How do I make this rotate on key down R?

Asked by 7 years ago
Edited 7 years ago
1mouse.KeyDown:connect(function(key)
2        if key == "R" then
3            NewTurret.Orientation = Vector3.new(X0,Y+90,Z0)
4        end        
5end)
0
Check if FE is on and if it compatible. Seshimazi 0 — 7y

2 answers

Log in to vote
1
Answered by 7 years ago

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.

1local UIS       = game:GetService("UserInputService");
2 
3UIS.InputBegan:Connect(function(key, gpe)
4    if (not gpe) then
5        if (key.KeyCode == Enum.KeyCode.R) then
6            NewTurret.CFrame = NewTurret.CFrame * CFrame.Angles(0,math.rad(90),0);
7        end
8    end
9end)

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

1local UIS       = game:GetService("UserInputService");
2 
3UIS.InputBegan:Connect(function(key, gpe)
4    if (not gpe) then
5        if (key.KeyCode == Enum.KeyCode.R) then
6            NewTurret:SetPrimaryPartCFrame(NewTurret.PrimaryPart.CFrame * CFrame.Angles(0,math.rad(90), 0));
7        end
8    end
9end)

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.

0
No, not really. You could use math.pi, or write out the radians yourself. hiimgoodpack 2009 — 7y
0
Mind explaining to me where I screwed up @hiimgoodpack? I said that CFrame.Angles() takes radian values as an input. math.pi would return a number which is considered a radian, etc. -- I never made a mistake with this post. AstrealDev 728 — 7y
Ad
Log in to vote
0
Answered by
LuaDLL 253 Moderation Voter
7 years ago

Maybe??

1local UIS = game:GetService('UserInputService')
2 
3UIS.InputBegan:Connect(function(key,gpe)
4    if key.KeyCode == Enum.KeyCode.R and not gpe then
5        local NewTurret = workspace.Part -- Remove this since you already have a variable for it
6        NewTurret.Orientation = Vector3.new(X,Y+90,Z)
7    end
8end)

Answer this question