Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I make this rotate on key down R?

		mouse.KeyDown:connect(function(key)
				if key == "R" then
					NewTurret.Orientation = Vector3.new(X0,Y+90,Z0)
				end			
		end)

1 comment

0

Check if FE is on and if it compatible.

2 answers

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.

2 comments

0

No, not really. You could use math.pi, or write out the radians yourself.

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.

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)

This discussion is preserved as it was. New answers, comments, and votes are closed.