Running client rotations

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

  1. how can I do such thing (on the client for smoothness) yet not cost this much performance
2 Likes

Are they all visible at once?? If not, don’t rotate models that can’t be seen

2 Likes

They are all visible at once. It’s not a big map.

2 Likes

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

2 Likes

Not sure there is any method that is more performant than this. Maybe try playing around with HingeConstraints and AngularVelocity

2 Likes

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!

1 Like

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 )

2 Likes

Can you send a video of what you’re doing? It should be smooth already. Also @8O8_guy has a really good idea, try that as well

1 Like

no his idea is not good. each model inside that folder has a pivot point.

as example it has a windmill that that script has to rotate

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.

You can also look into WorldRoot:BulkMoveTo() if you need further optimization.

1 Like

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.

1 Like

but will it automatically give the player full controll of the physics?

1 Like

If you need it to be shared, you need to have it on the server.

Are you going for a physical approach or do you just want it to be animated?

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

I would recommend using a animation controller for the best performance

  1. Animation controller movements are batched and will perform better then WorldRoot:BulkMoveTo()
  2. Animation controller will animate at different rates based on how far away it is from the camera

here are some links:

this is not a rig! read the post please

I did read the post

it does not need to be a rig all you need to do is add a motor6d nothing else

in the video I showed we use a door as a demo

that uses 2 parts and 1 motor6d and a animation controller

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)