local part = script.Parent
while task.wait() do
part.CFrame = part.CFrame * CFrame.fromEulerAnglesXYZ(0,0.05,0)
end
--we are basically rotating the part on the y axis in a loop which makes it looks like its spinning, the "0.05" controles the amount which it rotates so the speed
While it is a useful tutorial, it lacks something I find most (video) tutorials lack: Explanation.
âHow does it work?â, people following the tutorial will ask. And personally, I think it would be better to teach the concepts, the idea of how it works instead of blindly giving the finished script to others. While they probably got it to work, they wouldnât learn much from this.
On the script itself, a (possibly) smoother animation can be achieved with RenderStepped or Heartbeat over an infinite loop.
you donât actualy need a script to make a rotating part, you just have to put a BodyVelocity and make the part float, and then add AngularVelocity and boom, it rotate smoothely and just by anchoring the part, you can stop the rotation, and I think itâs also less lag intensive.
Why does this tutorial exist? One of the first things I did with Roblox was making a spinning gear, I was like âHey, I can just change the orientation of this part with a while loop!â My version was worse, but yours still isnât much better. You donât explain anything at all, the formatting is a bit weird, the methods used are weird, this is something that I would find on codegrepper.
A better script would be this:
local RunS = game:GetService("RunService") --// We use RunService so we can run code every frame
local body = script.Parent --// We get the body that we will rotate
local speed --// The speed (which we will use later)
local dir = Vector3.new(0, 1, 0) --// The direction
RunS.Heartbeat:Connect(function() --// Use RenderStepped if you're doing this on the client
speed = 0.1 --// The acuall speed. It's here so we can change the speed later
local v = dir * Vector3.new(speed, speed, speed) --// Calculating the proper vector for the speed
body.CFrame *= CFrame.fromEulerAnglesXYZ(v.X, v.Y, v.Z) --// Rotating the body with CFrames
end)
When making tutorials, youâve got to explain every last detail, so my example isnât perfect, but it still at least shows you why we do stuff like:
Itâs often better to make the part rotate on the client, rotating platforms or walls would be better to rotate from a server script so all the players are synced.
For network performance, decorative items like coins, gears, etc could be tagged âLocalRotatingâ, then using the Collection Service and a local script like so
--!strict
local collectionService: CollectionService = game:GetService("CollectionService")
-- we have to dynamically collect our rotating parts, they may be loaded and unloaded for the client whenever!
local rotationObjects: {Part} = {}
-- gets loaded rotation parts
for _, taggedObject in pairs(collectionService:GetTagged("LocalRotating")) do
table.insert(rotationObjects, taggedObject :: Part)
end
-- to get future rotation parts
collectionService:GetInstanceAddedSignal("LocalRotating"):Connect(function(newpart: Part)
if table.find(rotationObjects, newpart) == nil then
table.insert(rotationObjects, newpart)
end
end)
-- to remove rotation parts as they are unloaded
collectionService:GetInstanceRemovedSignal("LocalRotating"):Connect(function(oldpart: Part)
local index = table.find(rotationObjects, oldpart)
if index then
table.remove(rotationObjects, index)
end
end)
-- continually apply the rotation
while true do
for _, rotator in ipairs(rotationObjects) do
-- using an attribute to control speed on a object to object basis
local amount = rotator:GetAttribute("RotationSpeed") or 0.1
rotator.CFrame = rotator.CFrame * CFrame.fromEulerAnglesXYZ(0,amount,0)
end
wait()
end
Using a while loop is never a good practice (RenderService is better). Apart from that, why donât you just use an infinitely repeating tween, thatâd be much more effective. Also, you shouldnât use a server script for this, it eats up the servers performance, stick to local scripts for these things.
I think that using the while true loop is a horrible idea. I prefer replacing it with RunService.heartbeat because its actually less lag intensive and runs the loop depending on the FPS the server and client is running on.