Help with Tweening a Pendulum Between -180 and 180 Degrees

Hi everyone,

I’m working on a pendulum effect in Roblox and am trying to make it swing smoothly between -180 and 180 degrees. I’m using TweenService to create the back-and-forth motion, but I want to ensure the pendulum rotates correctly in both directions without manually creating multiple tweens just like in Obby But You’re On a Bike.

Here’s the code I currently have:

--[Services]--
local TweenService = game:GetService("TweenService")

--[Tween Information]--
local TweenInfo = TweenInfo.new(1, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, -1, true, 0) -- Looping indefinitely with reversing enabled

--[Create Tween]--
local targetPart = script.Parent
local Tween = TweenService:Create(targetPart, TweenInfo, {CFrame = targetPart.CFrame * CFrame.Angles(0, 0, math.rad(180))})

--[Play Tween]--
Tween:Play()

I set the reverses property to true and the repeatCount to -1 to make it go back and forth, but I’m unsure if this is the best approach for achieving smooth swinging. The goal is to have the part rotate indefinitely from -180 to 180.

2 Likes

experiment with easingstyle. a few of them slow down gradually when reaching their target

I tried doing this but I think something is wrong with the degrees because it is going in an unexpected direction:


It is supposed to go like this:

I’m unsure what is wrong…

no idea what’s happened there.

The pendulum uses Rod and Hinge constraints because, previously, I tried using Rope and, sadly, it didn’t work. This was the best approach that I could replicate just like in the game I mentioned in my topic, but something is not going right with the degrees

try a start, middle, end position so it will always go in the desired direction

I think its cuz the rotation is 180 where you are looking for 90 degrees to go opposite, if the pendulum starts at 45 degrees. Also, it should be in a local script to be more smooth for the players.

if your finding them go wonky try using welds and setting the other objects than that white cylinder unanchored. see if the cylinder alone starts to rotate correctly, it might be a problem with the build.

I tried doing this and the pendulum still looks bizarre as it goes in another direction that I don’t wish to go:

This is the whole idea that I want to achieve:

360_F_413009447_uHSB3aBYV8eT5pyZBlcfiiCc5lkFjzWU

I tried using Weld and Rod and they didn’t work, just so anyone knows

Have you made sure that the problem is not the pendulum itself? Try experimenting with a single and big part first to see if it rotates correctly.

Rods pivot at the Attachments so the ball will rotate around the attachments.
Just use a long Cylinder Part as your ‘rope’ and weld it and the ball to the pivot Part. Tween the Anchored pivot Part with your script.
Also try 179 degrees. If you use exactly 180 it may sometimes go the other direction around if it goes .0000000001 degrees past the end value. The engine sees that as the shortest number of degrees to get to the goal.

Why not just use physics to do this?

Anyways for this you usually do something like:

local ts = game:GetService("TweenService")

local info = TweenInfo.new(2,Enum.EasingStyle.Cubic,Enum.EasingDirection.InOut,-1,true,0)

local value = Instance.new("NumberValue")

value.Value = -90

local cf = workspace.Anchor.CFrame:ToObjectSpace(workspace.Bob.CFrame)

ts:Create(value,info,{Value = 90}):Play()

value.Changed:Connect(function()
	workspace.Bob.CFrame = workspace.Anchor.CFrame*CFrame.Angles(math.rad(value.Value),0,0)*cf
end)

Why are you telling that to me? It’s not my job to do everything for the OP like they are a child. They could do it themselves. My job is to just give them the concept. Like every forum member should. It’s in the rules themselves.