How do I make a Piston?

So like I made “Rotators” and “Spinners” with Motor6D, but I wondered if there’s a simple way to make a Piston (example: Minecraft Piston).

I’ve seen some designs but they only work if the part is anchored.

1 Like

Why use Motor6Ds for these instead of HingeConstraints? As for pistons, Cylindrical or Prismatic constraints are best

1 Like

You can try a prismatic constraint
The piston head can be un-anchored so physics are applied
and the piston block can be anchored.

Here is my gift to you:
Gift.rbxm (8.8 KB)

Remember that only un-anchored and parts with mass can be pushed!

1 Like

Because Motor6D has “DesiredAngle”, how would Hinges be better than that, also am trying the Rod Constraint right now and it kinda works well.

I need something that works and functions as its attached to the player’s character

Thank you but I think the code is little complicated.

I only need a minimum/maximum length attribute, and a function that turns it on/off

Its not even 50 loc

So Called Complicated Code
-- Automatic Pistons with delay
local pistonsfolder = workspace.Pistons

--config each piston
local pistons = {
	{
		modelName = "Piston_1",
		speed = 32,
		force = 2e6,
		delay = 1,
		startDelay = 1
	},
--Add more here
}

local function getPrismatic(pistonModel)
	local prism = pistonModel:FindFirstChild("Ram", true)
	if not (prism and prism:IsA("PrismaticConstraint")) then
		for _, desc in ipairs(pistonModel:GetDescendants()) do
			if desc:IsA("PrismaticConstraint") then prism = desc break end
		end
	end
	prism.ActuatorType = Enum.ActuatorType.Servo
	return prism
end

for _, config in ipairs(pistons) do
	local model = pistonsfolder:WaitForChild(config.modelName)
	local constraint = getPrismatic(model)
	constraint.ServoMaxForce = config.force
	constraint.MotorMaxForce = config.force
	constraint.Speed = config.speed
	
	local low, high = constraint.LowerLimit, constraint.UpperLimit
	
	task.spawn(function()
		task.wait(config.startDelay)
		local target = high
		while true do
			constraint.TargetPosition = target
			task.wait(config.delay)
			target = (target == high) and low or high
		end
	end)
end

Don’t be scared of the code inside exist everything you need

Yes I see, I think I can use this code on another game I have. But RodConstraint is gonna be good enough I think.

Like performance speaking Rod might be worse idk

I am shaking and trembling because of you

XD, sorry

This is just too simple and easy

local rod = script.Parent.Backpart.RodConstraint

local minimum_length = 0.3
local max_length = 50

rod.Length = max_length

That doesn’t even do what you need it to do

Does not get simpler than this

local piston = script.Parent
local constraint = piston:FindFirstChild("Ram")

local config = {
    minLength = 0.3,
    maxLength = 5.0,
    speed = 16,
    force = 1e6,
}

if constraint and constraint:IsA("PrismaticConstraint") then
    constraint.ActuatorType = Enum.ActuatorType.Servo
    constraint.ServoMaxForce = config.force
    constraint.Speed = config.speed
    constraint.LowerLimit = config.minLength
    constraint.UpperLimit = config.maxLength
end

local isOn = false

local function extend()
    if constraint then
        constraint.TargetPosition = config.maxLength
        isOn = true
    end
end

local function retract()
    if constraint then
        constraint.TargetPosition = config.minLength
        isOn = false
    end
end

local function toggle()
    if isOn then
        retract()
    else
        extend()
    end
end

Hinges also have a Motor and Servo mode, where you can achieve constant rotation and set an angle very easily. Plus it’s physics based so no issues on that

1 Like