As the title says: How would I get a part to rotate?
All answers help.
You could do
local part = "replace with path to your part"
part.Orientation = Vector3.new("Rotation on X axis", "Rotation on Y axis", "Rotation on Z axis")
or a little bit more complicated:
local part = "replace with path to your part"
part.CFrame = part.CFrame * CFrame.Angles(math.rad("Rotation on X axis (in degrees, if you put it in radians remove the math.rad() part"), math.rad("Rotation on Y axis (in degrees, if you put it in radians remove the math.rad() part"), math.rad("Rotation on XZaxis (in degrees, if you put it in radians remove the math.rad() part"))
How do you want âa part to rotateâ? By CFraming it, tweening it, physically using Constraints, with scripted input controls?
There are so many choices and options, itâd help if you told us exactly what and how you want something to rotate.
For example, a wheel on a car, a ceiling fan, a spinning globe, a pet around a player, a moon around a planet, a doorknob, an airplane control system, a Part that rotates to point at another moving PartâŚ
I want it to rotate on the Z AxisâŚ
You would do something like this
degreesToRotate = 90
part.CFrame = part.CFrame * CFrame.Angles(0, 0, math.rad(degreesToRotate))
-- CFrame.Angles takes values in radians, so math.rad() is required for conversion.
You can use RunService to make the Rotation more smooth:
game:GetService("RunService").Stepped:Connect(function() -- you can use Heartbeat, but i recommend Stepped
part.CFrame = part.CFrame * CFrame.Angles(0, 0, math.rad(90))
end)
However it depends if you want a loop for a one time change
If you want the loop go for a certain amount of times, use a For loop:
for i = 1,10 do -- 1,10 means that the script will run it 10 times, the Variable i is optional
wait() -- Add a wait or else the game will crash
part.CFrame = part.CFrame * CFrame.Angles(0, 0, math.rad(90))
end
The For Loop is less smooth but more useful, I believe you can also use TweenService for it but i dont think it belongs here, plus its also a waste of time to create a Tween just for it to rotate.\
Other Methods:
repeat -- This type of loop is never used, Its here in case you do use it
wait()
part.CFrame = part.CFrame * CFrame.Angles(0, 0, math.rad(90))
until false
-- You can use a Bool here just like Below
repeat
wait()
part.CFrame = part.CFrame * CFrame.Angles(0, 0, math.rad(90))
until "Your Variable" == "true, false, or nil"
while wait() do
part.CFrame = part.CFrame * CFrame.Angles(0, 0, math.rad(90))
end
-- Another thing you can do with a while loop is set a bool for when you want to turn it off
while "Your Variable" == "true, false, or nil" and wait() do
part.CFrame = part.CFrame * CFrame.Angles(0, 0, math.rad(90))
end
Is OP asking how to rotate a part to a different rotation, or are they asking how to make a part that rotates continuously?
While your suggestions do work, I donât think most of them are efficient. If you want to have a part that keeps spinning forever, itâs more practical to simply make the part into a spinning part using physical constraints, removing the need to write code. If you did want to change the rotational speed, you could use a script to modify the rotating constraint, instead of having to change a CFrame.Angles value in a loop.
TweenService is very useful for many situations, including this one. I donât know how it could be a âwaste of timeâ because there doesnât seem any other efficient ways to smoothly rotate a part with customizable parameters. If OP is asking how to rotate a part to another orientation only once, TweenService could come in handy here. In this case, I would use it like this:
local ts = game:GetService("TweenService")
function tween(object, info, goal)
ts:Create(object, info, goal):Play()
end -- This exact function is useful to tween on demand in one line
tween(part, TweenInfo.new(1), {CFrame = part.CFrame * CFrame.Angles(0, 0, math.rad(90))}
@MooMooManager
It doesnt have to be efficient, It just has to rotate, either way, the posts highlights ways to make a part rotate, etc, for however long you want, i dont think it needs to be efficent, plus adding constraints is Also a way for a part to spin but if the player has bad connection to the server, the Constaint will lag out and probably revert to its last known space, so its better to use the script for it. TweenService is another way but its tedious and a waste of time just for a part to rotate, you only need one line of code for it to only run once
This is the scripting support category, Iâm sure it would be at least somewhat reasonable to expect efficient solutions for problems and not less efficient ones.
You can avoid bad constraint physics by setting the spinning partâs network ownership to the server (info here) so that players cannot control the partâs physics when near it. Alternatively, you can make the spinning part local to the player which makes them see the part spin smoothly.
Once again, we donât know if the original poster wants a part to rotate continuously or rotate only once.
It says it in the topic, either way, the part rotates
@MooMooManager
You have a point but still, if it rotates it rotates, doesnt matter if its efficent or not, it still helps
Seriously, check the reply above his⌠itâs right there
Doesnt really change anything about what the topic is, if it rotates on any coord, it rotates
Yes, you want it to rotate on the z axis⌠We get that.
You didnât answer ANY of my questions though.
It would help everyone here a whole lot more if we knew WHAT you were rotating and for WHAT purpose. Not just âa partâ, not just âon the Z axisâ. Please be descriptive!
Do you want it to constantly rotate on the Z axis? Do you want it to be a controlled rotation by player input? Does it need to be Anchored or does it move with another object?
Help us help you!
@DasKairo @SimplySquidtastic @MooMooManager Iâm not saying ANY of your answers here are wrong, but OP hasnât even told us what they are trying to do other than make it spin in the Z axis. Itâs entirely possible that none of our answers are what they actually need.
even tho they said they need it on the Z axis, they can also know how to make the objects spin, thats what we are explaining, how to make objects spin / rotate. thats what it says on the topic, so thats what we are helping with
if it rotates, it works. doesnt have to be on the Z axis, the script does the same no matter what
I get that and all of everyone elseâs help on the topic, but when someone asks a really vague question you need to know what they want.
What if their issue is a wheel that spins on the Y axis on their car and they want it to spin on the Z axis?
I know.
Then we can take all the time in the world to argue a hundred answers but itâs like saying how can I make a part go up?
Unless you know what the person is actually referring to there are so many ways to answer it. We need to know what they are trying to do before we can actually give a good answer.
This is such a vague question youâd have better luck searching YouTube, the DevHub, or even just community tutorials.
This category is more so for help with code you already have.
he asked, thats what he got in return, plus stop arguing here, open a DM since this is flooding the post