Spinning Objects

Hello, in this tutorial I wanna show you how to make spinning objects
like this:

  1. Get your Object/Part (i would prefer a gear)

  2. Create a Script Inside of the Part
    8ece6e788737826ce738171acdebd8ed

  3. Write or paste this inside of the script

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
  1. You are finished :wink:.

This was my first tutorial, hope you like it.

Have a great day! :smiley:

37 Likes

Uhm, this is just simple but great and short tutorial.
This should be useful for beginner developers.

6 Likes

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.

9 Likes

ok thanks i will consider that in my next tutorial because this was my first one,
so thanks for your advice
Have a great day

2 Likes

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.

10 Likes

ok this is actually good to know

3 Likes

this is good, because if beginner developers are joining, they could know they first script.
and this is easy to script.

4 Likes

im still new to CFrame can someone tell me what fromEulerAnglesXYZ do?

2 Likes

fromEulerAnglesXYZ is rotating Parts. doesn’t moving parts, or anything.

2 Likes

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:

local a

if true then
    a = 1
end
14 Likes

you don’t even need a script for this, bodyvelocities with angularvelocity should do the work

3 Likes

should work too. i’m agree, this is working too.

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
5 Likes

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.

well since it’s a begginer tutorial, server to client replicating is not begginners friendly.

Uh there’s no need for server replication in most cases. Tower of hell for example doesn’t do it.

4 Likes

If you’re going to do a tutorial, at least be detailed and explain what what each line of the code do.

2 Likes

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.

2 Likes

Instead of task.wait() can you use wait() or no?

1 Like