How to make coins rotate smoothly without while loops

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? rotating coins smoothly with cframe.

  2. **What is the issue? ** so the issue is that I want to make my coins rotate smoothly but since I have thousands of coins in my game I don’t want to create that much scripts and use that much while loop.

  3. What solutions have you tried so far? I can’t come up with an idea.

Use tween with repeating math.huge

local TweenService = game:GetService("TweenService")

local Tweeninfo = TweenInfo.new(PutTimeHereInSeconds, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, math.huge)
-- Use -1 instead of math.huge

TweenService:Create(CoinMeshPart, Tweeninfo, {CFrame = CoinMeshPart.CFrame * CFrame.Angles(0, 0, math.rad(90))}):Play()

Might be errors, but I’m doing this out of memory. The CFrame might be wrong too.

6 Likes

Hmm that is actually really good idea but I will still have to use 3000scripts for example and 3000 tweenservice wouldn’t that make the game slower?

I’m not sure.

You can do this tweening on localscript only, so you won’t destroy server performance.

And no, you don’t have to use 3000 scripts. Learn OOP :slight_smile:. One script can tween all 3000 coins.

1 Like

Oh yea idk OOP yet thank you very much.

Do not use math.huge for this, use -1.

4 Likes

Looks like I learned something new today too.

1 Like
local coins = workspace.Coins:GetChildren() -- put the coins in a model called "Coins" in Workspace

while true do 
	for _,coin in next, coins do 
		coin.CFrame *= CFrame.Angles(0, math.rad(0.2), 0)
	end 
	game:GetService("RunService").Heartbeat:Wait()
end 

That’s how I’d personally do it.

3 Likes

I use tweening a lot but I wouldn’t use it for things like that because you can control CFrame animations better, and calling TweenService always puts slightly more load on the server. I’m not saying Tweening is bad, but you don’t always need it for simple things like that. For value tweening, TweenService is a lot more practical, though.

Yea I was thinking of using for loops but I was not 2 sure that it was going to work since it had to rotate 1coin then go to 2nd but now I am assuming that it happens instantly .
Edit:btw is “next” same as pairs? Or os it something new??

Yeah, it happens more or less instantly. You would do the for loop for all the coins, then you would wait using RunService or Swait for speed purposes

Next is very similar, and ultimately it’s not a huge deal which you use. Next loops through the table until a value is nil, and when you’re using :GetChildren(), there’s really no difference. However, I think next goes through the table one at a time, which could be less laggy, but don’t quote me on that. For your purpose, it really doesn’t matter.

1 Like

Okay thanks for the info. (Random text cuz it has to be minimum 30letter lol)

but don’t quote me on that.

Just a heads up that is not true about ‘next’; “next”, “pairs”, and “ipairs” in lua behave and are the same (but ipairs only returning 3 index’s and its values)

You can perform a clamped pairs using the following code:

function pairs(t)
    return next, t, nil
end