Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How can I make this wheel rotate smoothly with "for i"?

So, i've created a pipe wheel, and when the player presses "E" then it rotates but now, it just spins somewhere instantly, not smoothly. Here's the code:

local prox = script.Parent.wheel.Union.ProximityPrompt

prox.Triggered:Connect(function()
	for i = 1, 100, 1 do
		script.Parent.wheel.Union.CFrame = script.Parent.wheel.Union.CFrame * CFrame.fromEulerAnglesXYZ(0.5,0,0) * CFrame.new(0, 0, 0)

	end
end)

1 comment

0

Maybe try an animation for the wheel if that's possible

2 answers

✓ Accepted answer

You need to add a wait() after one run-through of the loop (line 6). Currently it does not wait after doing a cycle so it will instantly run the loop and finish within a fraction of a second. If it is still too fast, increase the wait time, but the more you increase it the choppier it will become.

A more elegant solution would be to use tweening, which is a built-in system for smooth changes.

Not sure I got the pathing correct since I was using a part with a script under it for my setup but otherwise it works

local prox = script.Parent.wheel.Union.ProximityPrompt
local tweenService = game:GetService("TweenService")

local rotationTime = 5

local tweeningInformation = TweenInfo.new(rotationTime, Enum.EasingStyle.Linear, Enum.EasingDirection.In, 0, false)

prox.Triggered:Connect(function()
	local tweenProperties = {
		CFrame = script.Parent.wheel.Union.CFrame * CFrame.Angles(math.rad(180),0,0) * CFrame.new(0, 0, 0)

	}
	local Tween = tweenService:Create(script.Parent.wheel,tweeningInformation,tweenProperties)
	Tween:Play()
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.