Rotating part not working

I am trying to make a coin that is duplicated from Replicated Storage into the workspace that spins continuously. This may seem simple however it is not working. Here is my code:

local coin = script.Parent

local increment = 360/20

while true do
	for deg = 0, 360, increment do
		coin.CFrame = CFrame.new(coin.Position) * CFrame.Angles(0, math.rad(deg), 0)
		wait(0.1)
	end
end

Thanks in advance,
WolfTamer894

1 Like

That’s because each time it iterates, it makes a new CFrame with a rotational value of 0,0,0 offset by 360/20 degrees.

To fix it, multiply the coin’s CFrame by angles(0,math.rad(deg),0) instead:

coin.CFrame *= CFrame.Angles(0, math.rad(deg), 0)

Also FYI, you can create a smoother rotation by using task.wait() instead of wait().
FYI part 2, you don’t need the for loop either (unless you are doing something with the deg value), you can simply do

while true do
    coin.CFrame *= CFrame.Angles(0, math.rad(increment), 0)
    task.wait()
end

The script is still not working. Also, it is worth noting that I want the coin to always be spinning.

Could you show the script you have right now?

Here it is!

local coin = script.Parent

local increment = 360/20

while true do
	for deg = 0, 360, increment do
		coin.CFrame *= CFrame.Angles(0, math.rad(deg), 0)
		task.wait(0.01)
	end
end

Hmm, the script’s parent is a part, right? Could it be spinning on the wrong axis?

I have found out that the issue lies within the Replicated Storage cloning.

Here is the cloning code:

local coinClone = storage:WaitForChild("Coin")
	local coinClone1 = coinClone:Clone()
	local coinClone2 = coinClone:Clone()
	local coinClone3 = coinClone:Clone()
	
	coinClone1.Name = "CoinClone1"
	coinClone1.Parent = workspace
	coinClone1.CFrame = CFrame.new(Vector3.new(HumanoidRootPart.Position.X, HumanoidRootPart.Position.Y + 5, HumanoidRootPart.Position.Z))
	
	coinClone2.Name = "CoinClone2"
	coinClone2.Parent = workspace
	coinClone2.CFrame = CFrame.new(Vector3.new(HumanoidRootPart.Position.X, HumanoidRootPart.Position.Y + 5, HumanoidRootPart.Position.Z))

	
	coinClone3.Name = "CoinClone3"
	coinClone3.Parent = workspace
	coinClone3.CFrame = CFrame.new(Vector3.new(HumanoidRootPart.Position.X, HumanoidRootPart.Position.Y + 5, HumanoidRootPart.Position.Z))

I don’t see anything wrong with that, are you sure it’s the cloning thing that’s causing it? The only thing I can think of is that coinClone is a model, if that’s the case, you’d probably have to use :PivotTo instead of just assigning the CFrame. Any output errors?

No output errors, and it is not a model.

Is your rotation script a server script or is it a local script?

It is a server script, not local script

The only thing I can think of now is that the coin is rotating so fast that you can’t see it.

Try this:

local coin = script.Parent

local increment = 360/20

print('starting spin')

while true do
	coin.CFrame *= CFrame.Angles(0, math.rad(increment), 0)
	task.wait()
end

If it still isn’t working, is the starting spin thing being printed?

Still nothing happening, not even a printing message

If you look inside the part, is the script there?

Yes, there is a script in the duplicated part

Okay that’s really weird. Is the part being cloned by a local script?

It is being cloned by a local script, wait, ah! I think that’s it! Let me test it with a remote event and see if that works

1 Like

Yeah that’s definitely the issue then. I suggest doing something like this:

local increment = 360/20
-- not sure how you're defining humanoidRootPart but ensure you are defining it somewhere
local coins = {}
local coinClone = storage:WaitForChild("Coin")
for i = 1,3 do
    coins[i] = coinClone:Clone()
    coins[i].CFrame = humanoidRootPart.CFrame * CFrame.new(0,5,0)
    coroutine.wrap(function() -- a coroutine allows us to create another thread which allows multiple asynchronous functions to run at once, sort of like creating another script
        while coins[i] do -- while the coin exists,
            coins[i].CFrame *= CFrame.Angles(0,math.rad(increment),0)
        end
    end)()
end

Then if you need to reference a specific coin, you could do it by indexing it through the coins table. For example, coin 1 would be coins[1], if you want to destroy it, you could do coins[1]:Destroy()

Thanks for the help 7z99! Greatly Appreciated

1 Like