How would I be able to make reciprocating engines using CFrame?

So, I’m not sure how to start the scripting side of things.

But I need to script a reciprocating engine, found on [random example] titanic, and I need it to work with tween service, so it might need to use math which i have nearly no clue how to use.

I haven’t gotten to try a solution because I haven’t started, because I have no clue HOW to start.

Engines need to look like the GIF below. [focus on the pistons going up and down, and how the thing they’re connected to spins]
vvvvvvvvv

HingeConstraint + AllignPosition probably

sp=script.Parent --A model holding this script, piston (the up-down part), spinner (the circular part) and connector (the part connecting the two)

ts=game:GetService("TweenService")
period=2 --time for 1 cycle in seconds
twi=TweenInfo.new(period/4,Enum.EasingStyle.Linear,Enum.EasingDirection.InOut,0,false,0) --tween info for 90 degree rotation (because 360=0 and may not make it move)
rotval=Instance.new("NumberValue") --tweens only work on objects so we use a numbervalue object

originalsy=sp.spin.attach.Position.Y --original Y position of the attaching point on the spinner
originalpy=sp.piston.Position.Y      --original Y position of the piston


rotval.Changed:Connect(function()
	sp.spin:PivotTo(CFrame.new(sp.spin:GetPivot().Position)*CFrame.Angles(0,0,math.rad(rotval.Value)))                                                  --rotate the spinner to match rotation value
	sp.piston.Position=Vector3.new(sp.piston.Position.X,originalpy+(sp.spin.attach.Position.Y-originalsy),sp.piston.Position.Z)                         --move the piston up and down with the top of the spinner, subtracting the change in Y of the attacher from the piston's Y so they both have the same change in Y
	sp.connector.CFrame=CFrame.new(sp.spin.attach.Position,sp.piston.Position)*CFrame.new(0,0,-(sp.spin.attach.Position-sp.piston.Position).Magnitude/2)--move the connector to the middle and face it at the piston
	
end)
tween=nil --value for tween so we wait till it finished in the loop to start it again
while true do
	
	
	tween=ts:Create(rotval,twi,{Value=rotval.Value+90})--make the tween to rotate 90 degrees
	tween:Play() --play the tween
	tween.Completed:Wait() --wait for the tween to finish then restart (we wait 1/4 the period since we go 1/4 of a full rotation)
	
end



This may be a possible solution.

We have a model like shown above. Script has the code. Piston is the part going up and down (blue part). Spin is the spinner (model including green part + gray part). Circle is the circle part (actually it’s the green square in the image but whatever) and Attach is the part on the top of the spinner that is connected to the piston (the gray square in the image). Connector connects the piston to the attach part (brown thing).

We use tweening to spin the entire spinner using a NumberValue. As the NumberValue updates, update the rotation of the spinner. Also, update the Y position of the piston based on the Y position of the Attach. Use some CFrames to put the connector in between the two and face it at the piston (it is long on the Z axis, so it would look like it connects them).

This is only one of many possible solutions. There are probably more efficient ways to do it using constraints and stuff, but this one uses tweens and anchored parts.

oh I’ve made this before like 3 times it mainly needs prismatic constraints, hingeconstraints, yeah that’s about it

I need to use CFrame, not physics stuff i.e. constraints

I’ll try this later and will let you know if it works

The tween method worked, tysm!