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 6 years ago
Edited 6 years ago
        mouse.KeyDown:connect(function(key)
                if key == "R" then
                    NewTurret.Orientation = Vector3.new(X0,Y+90,Z0)
                end         
        end)
0
Check if FE is on and if it compatible. Seshimazi 0 — 6y

2 answers

Log in to vote
1
Answered by 6 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.

local UIS       = game:GetService("UserInputService");

UIS.InputBegan:Connect(function(key, gpe)
    if (not gpe) then
        if (key.KeyCode == Enum.KeyCode.R) then
            NewTurret.CFrame = NewTurret.CFrame * CFrame.Angles(0,math.rad(90),0);
        end
    end
end)

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

local UIS       = game:GetService("UserInputService");

UIS.InputBegan:Connect(function(key, gpe)
    if (not gpe) then
        if (key.KeyCode == Enum.KeyCode.R) then
            NewTurret:SetPrimaryPartCFrame(NewTurret.PrimaryPart.CFrame * CFrame.Angles(0,math.rad(90), 0));
        end
    end
end)

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 — 6y
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 — 6y
Ad
Log in to vote
0
Answered by
LuaDLL 253 Moderation Voter
6 years ago

Maybe??

local UIS = game:GetService('UserInputService')

UIS.InputBegan:Connect(function(key,gpe)
    if key.KeyCode == Enum.KeyCode.R and not gpe then
        local NewTurret = workspace.Part -- Remove this since you already have a variable for it
        NewTurret.Orientation = Vector3.new(X,Y+90,Z)
    end
end)

Answer this question