I have this script that rotates all models on a folder
local To_Rotate = workspace:WaitForChild("ThingsToRotate")
local rad = math.rad
local Angles = CFrame.Angles
local new = CFrame.new
local speed= 18
local Table = To_Rotate:GetChildren()
To_Rotate.ChildAdded:Connect(function(child)
table.insert(Table,child)
end)
game:GetService("RunService").Heartbeat:Connect(function(dt)
for _,Object in (Table) do
if Object.PrimaryPart then
Object:PivotTo(Object.PrimaryPart.CFrame*Angles(0,rad(dt*speed),0))
end
end
end)
this script runs fine but the performance is very bad at larger scales
how can I do such thing (on the client for smoothness) yet not cost this much performance
Keep in mind this script uses 5% activate on mobile which is a lot and I need for other stuff.
so is there a constraint or any other way to do the same idea
Unanchor everything and weld it to one invisible part that is anchored, then rotate that invisible part only, this takes advantage of Roblox’s physics solver which will handle applying transformations to all the other children without the heavy performance hit (updating lots of properties is expensive). Hope this helps!
I managed to make it work with hinge constraint but how would I make it so it’s smooth for the client as he handles it instead of server ( other players need to handle it too so I can’t give the network ownership for 1 playaer )
You should weld the parts of your models together and set one primary part’s CFrame, it avoids the cost of updating the CFrame properties of descendant parts.
If you’re using HingeConstraints and want it to be a client visual, you should have the client create the parts themselves, you can store the models in a place like ReplicatedStorage.
no I don’t have to run it on the server to make it shared it would not be the best in terms of animation smoothness. I can store a number on the server that stores current rotation and once a player joins he gets that data and starts the animaton from there.
but the approach of using :PivotTo is expensive since iIhandle it and not the phyiscs engine.
So again how would I let the physics engine handle it and not use :PivotTo()
local folder = workspace:WaitForChild("ThingsToRotate")
local rs = game:GetService("RunService")
local speed = 18
local rad = math.rad
local ang = CFrame.Angles
local objs = {}
for _, o in ipairs(folder:GetChildren()) do
if o.PrimaryPart then
objs[#objs+1] = o
end
end
folder.ChildAdded:Connect(function(c)
if c.PrimaryPart then
objs[#objs+1] = c
end
end)
local angle = 0
rs.RenderStepped:Connect(function(dt)
angle += dt*speed
local c = ang(0, rad(angle), 0)
for _, o in ipairs(objs) do
o:PivotTo(o.PrimaryPart.CFrame * c)
end
end)