Got a donut morph, and trying to make it spin when the player moves but it’s not working too well. Here’s the current code:
local Donut = script.Parent
local Doing
Donut.Humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(function()
if not Doing then
Doing = true
while Donut.Humanoid.MoveDirection ~= Vector3.new(0, 0, 0) do
Donut.Donut.CFrame *= CFrame.Angles(0, math.rad(1), 0)
task.wait()
end
Doing = nil
end
end)
The rotation created by CFrame.Angles(0, math.rad(1), 0) will be in world space. You want it to be in the space of the donut. You should use CFrame.fromAxisAngle with the forward axis of the donut as the first arg.
The *= operator is going apply it as a post-multiply, so it’s a rotation in the Donut mesh’s local coordinate space.
You don’t want to do that. Assuming it’s welded with a Motor6D, the change would replicate and everyone will see it, but it will replicate and update erratically in a live game, at 20 FPS maximum and you’ll see the changes as property update gets processed, so you’ll visually see all the irrgularlity in your network latency. You’ve seen how this looks, I’m sure. “Laggy updates” is what the kids will say about it.
You could just use the Motor6D built-in ability to rotate smoothly by setting the desired angle to something big and use MaxVelocity to control the rotation speed, but IMO it would be even better to connect the Donut to the root part with a HingeConstraint with actuator set to Motor. It depends on whether the donut is collidable and whether you care about the physics engine knowing it’s rotating. Motor6D rotation happens outside the physics simulation step, as a rotationl update between frames. Hinge rotation is actually simulated and applied by the physics engine as a torque to achieve the desired speed. See what works best for you.
I’d also recommend doing something like: Donut.Humanoid.MoveDirection.Magnitude > 1e-2
as your velocity check, so that your donut ignores tiny velocities that you might get from something like a game controller whose thumbsticks don’t center exactly or are noisy. In general, floating point comparisons in code should have a bit of tolerance.