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
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
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
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?
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?
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()