Why is this so laggy?

I have a ferris wheel in my game, but the script that makes it spin is super laggy and has ~1% activity in the Script Performance tab.

while task.wait(.1) do  
	script.Parent:SetPrimaryPartCFrame( 
		script ['Parent'] : GetPrimaryPartCFrame() * CFrame.Angles(math.rad(0.12), 0, 0)
	);
end

How would I make this less laggy??

8 Likes

Try using GetPivot and PivotTo. Also try doing this on the client, you can make it smoother and you get less server lag. A .1 second while loop will inherently be laggy.

It is on the client, and I also kind of need it at 0.1 seconds.

1 Like

I think you could just use a CylindricalConstraints to spin a wheel, you do not need any script (just clone the ferris localy to make it work localy)

Probably use @Temateite’s solution. I can’t believe I forgot physics exist. Just add something like a BodyMover. (Or CylindricalContrainst). (yes, im struggling to type this since my pinky is numb) If this works give HIM the solution, he deserves it.

EDIT: MY PINKY IS NO LONGER NUMB YAY

0.1 of a second is about every 10 frames, so at the scale of a large Ferris wheel, the jumps in rotation could be major.

How is it laggy? You mean it doesn’t appear smooth? Or does it drop frames?

In any case, the way you made your code is also very poor, you should be storing things in variables to prevent unnecessary indexing as well as you should abandon deprecated features like SetPrimaryPartCFrame.

Try this instead.

local FerrisWheel = script.Parent
local ActualPivot = FerrisWheel:GetPivot()
local ActualRotation = CFrame.Angles(0,0,0)
local Increment = CFrame.Angles(math.rad(0.12), 0, 0)

while true do
	task.wait(0.1)
	ActualRotation *= Increment
	FerrisWheel:SetPivot(ActualPivot * ActualRotation)
end

Try tweening it instead of just changing the CFrame directly. This should help optimize it. Just make a loop, and play the tween animation whenever the loop is finished

An activity of 1% isn’t laggy at all, more than that is (like, the lower the better);
As for why your ferris wheel might be choppy might be because it’s unanchored?

I have… no idea why your code looks like that. To each their own, I suppose.

Anyway, you could use RunService and PivotTo, which are a better use case for this scenario.

local RunService = game:GetService("RunService")

RunService.Heartbeat:Connect(function(dt)
    script.Parent:PivotTo(script.Parent:GetPivot() * CFrame.Angles(math.rad(0.012 * dt * 60), 0, 0))
end)

Using RunService lets it spin at 60fps (or whatever your framerate is, which is why I added the * dt * 60, which will remove any speed changes from it running slower than 60fps.)

Besides that: 1% activity is good. That means only 1% of the CPU is being used to run your script. It was “laggy” in your eyes likely because you were only updating it every 0.1 seconds, or at about 6fps, rather than at 60.