What's the most efficient way to rotate Multiple objects at the same time,without lag or "jittery effects"

I have been trying to rotate “coins” at the same time , for players to collect .

I have tried researching and there are some topics somewhat relating to this, but those don’t explain alot
I read you can use heartbeat somewhere, but I don’t really know how to do that
I inserted these in every coin, 120 to be exact,but would inserting this script into every one of them cause alot of lag? if yes, then what can I do to improve?

game:GetService("RunService").Heartbeat:connect(function()

while true do

script.Parent.CFrame = script.Parent.CFrame * CFrame.fromEulerAnglesXYZ(0,0,.05)

wait()

end

end)

First Post, not that experienced, but learning so excuse me if the code is very wrong

10 Likes

I would like to ask, why are you using heartbeat and a while loop?

Anyways, you should not insert the same script in every single coin, but instead, put the coins into a folder named “Coins” or something along the lines of that, then do a for loop that’s client sided (because you don’t want lag, right?) that is rotating the coins. It should be very simple.

4 Likes

So should i not use a while loop?
If not then do I just remove it, and put the code in a local script in the folder,
but if I do that, then how will i reference it to all the coins in the folder at one time?
Also without while , and putting the code in a LocalScript, would it still work like its supposed to?

3 Likes

You are stacking two loops, a heartbeat loop, and a while loop.

If you do not understand how to reference all the coins in a folder, use a for loop with GetChildren() on the folder. It should be quite simple.
And yes, the code does work in a LocalScript.

5 Likes

You mean, like this?

for 1,15 do
local coin = script.Parent:GetChildren()
game:GetService("RunService").Heartbeat:connect(function()

	coin.CFrame = coin.CFrame * CFrame.fromEulerAnglesXYZ(0.01,0,0)
	wait()
end)
3 Likes

You did it wrong.

It should be something like this:

--this is untested
local RunService = game:GetService("RunService")

local Coins = workspace:WaitForChild("Coins")

local AllCoins = Coins:GetChildren()
Coins.ChildAdded:Connect(function(v)
	table.insert(AllCoins, v)
end)
Coins.DescendantRemoving:Connect(function(v)
	local Found = table.find(AllCoins, v)
	AllCoins[Found] = nil
end)

local rad = math.rad

RunService:BindToRenderStep("CoinRotation", Enum.RenderPriority.Camera.Value, function(dT)
	local Scale = dT * 60
	
	for _,coin in pairs(AllCoins) do
		coin.CFrame = coin.CFrame * CFrame.Angles(rad(.001) * Scale, 0, 0)
	end
end)
9 Likes

Ok, hearthbeat is a loop, like a while loop.

dont use loops into one loop.

4 Likes

i kept it in a local script, and changed some values, but nothing in the folder moved, its not working for some reason, could you show me a tested one?

What is your LocalScript’s parent?

the parent is the folder,Coins

You cannot parent a LocalScript to something that is not in one of these:image
It has to be in a tool or those parents, or else, it will not work. You cannot parent it into the workspace.

4 Likes

then do i put the code in a normal script instead? or parent the folder with the local script to starter player scripts

1 Like

This is also dangerous to do. It may work okay for rotation, but if you start moving the coins also, they would drift out of position due to floating point error if you did it that way. A better way is to save the initial CFrame and modify from there:

local originalCFrame = part.CFrame
local theta = 0
...later for the update
theta = theta + 0.001
part.CFrame = originalCFrame * CFrame.Angles(rad(theta), 0, 0)
3 Likes

but would this work client sided, in the starterplayerscripts? referencing to part.CFRAME, with part being workspace:FindFirstChild(“part”) , for example?

2 Likes

Have you considered tweens?
They require no loop and if in the TweenInfo repeatCount is set to math.huge then it’ll tween inifinitely.

2 Likes

Yes, it will. It won’t replicate back to the server, but it will update what the user sees, which is what you care about. That’s what you want anyways, if you want the coins to rotate smoothly, then they have to be rotated individually on each client, because not all the clients are exactly in sync, they’re playing over a laggy sometimes lossy network.

6 Likes

Please don’t do this. The wiki says to use -1 for the repeatCount for it to repeat indefinitely.

The looped effect is achieved by modifying the TweenInfo used in TweenService:Create. Specifically, when RepeatCount is set to be less than 0 the tween will play indefinitely. Also, setting Reverses to true will cause the tween to play in reverse once it has reached [its] destination. In combination this creates a looped effect.

The correct way to make a tween play indefinitely is to set RepeatCount to -1. Developers should avoid using large numbers (or math.huge) as a substitute as this is unstable and may stop working at any point.

9 Likes

I nearly marked that as a solution, that’s really helpful ,thanks
but it still didn’t work , look at the screenshots :

image

image

I added the script i found on developer hub,to my own

local cfold = workspace.coinsfold  --:WaitForChild("coinsfold")
local coins = cfold:GetChildren()

local TweenService = game:GetService("TweenService")
 
local parts = coins
 
local tweenInfo = TweenInfo.new(
	2, -- Time
	Enum.EasingStyle.Linear, -- EasingStyle
	Enum.EasingDirection.Out, -- EasingDirection
	-1, -- RepeatCount (when less than zero the tween will loop indefinitely)
	false, -- Reverses (tween will reverse once reaching it's goal)
	0 -- DelayTime
)
 
local tween = TweenService:Create(parts, tweenInfo, {Position = Vector3.new(0, 30, 0)})
 
tween:Play()
wait(10)
tween:Cancel() -- cancel the animation after 10 seconds

That all still didn’t work, what am I doing wrong?

2 Likes

You still have to tween them individually, you could use the for loop example from an earlier post or you could use ChildAdded (probably preferred in your case). You need to set the properties of the tween as well, in this case you’re moving it along the Y axis 30. Furthermore, you’re canceling the tween after 10 seconds at the bottom.

2 Likes

But how could I use tween info separately, for example in the case of my code,and even if i set it to -1 , wont that rotate infinitely

1 Like