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 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
repeat
is never used? Where are you getting this from?
imo it IS used
pretty much, people dont usually use it since While can do the same thing
I also don’t usually see people use it in code except those rare cases
Variable = true
repeat wait()
print("p")
until Variable == false
while Variable == true do
wait()
print("p")
end
Honestly, I do use it sometimes. It depends on how I’m thinking as I’m typing out the code.
Either way, they are loops no matter what, they do the same thing. loop. repeat
is the least used , while for
, and while
are used frequently.