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

How to rotate an object using mouse.KeyDown?

Im having trouble when rotating this part, it works fine for the first 3 rotates but when I try to rotate it again it just switches between two vector3 values.

So, the part starts off at (0,0,0) I press "r" once and it goes to (0,90,0) on the second click the rotation is: (-180,-0,-180) on the third click the rotation is: (-0,90,0) on the fourth click the rotation is (180,-0,-180)

and it just keeps doing (180,-0,-180) and (-0,90,0) after that..

Is there a better way to rotate a part using a script??

This is a part of my local script and its for a build preview

mouse.KeyDown:connect(function(key)
	
	for i,child in pairs(HQ:GetChildren()) do 
	
		if child:IsA "BasePart" then 

	key = key:lower()
		
if key == 'r' then
			
		child.Rotation = child.Rotation + Vector3.new(0,90,0)    --Rotate (Works ok for first couple times then theres a problem ....Eventually I need this to repeat so I can keep rotating it until I like where it is)
		
end
end
end
end)

Thanks for helping!

2 answers

✓ Accepted answer

To rotate a part, you should use CFrames, like this:

-- Line 11:
child.CFrame = child.CFrame * CFrame.Angles(0, math.rad(90), 0)

You may also want to consider using the UserInputService rather than Mouse.KeyDown, as KeyDown is deprecated.

I'm new, but I'll take an educated guess. Sorry if I'm wrong. Maybe throw in a while?

3 comments

0

This should be a comment, not an answer.

0

You need more reputation to comment. Just try not to post answers unless you know the answer, and you can explain it properly.

0

Sorry, I wont do that again

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