I am trying to make a piston system but I wont get it working. I can’t seem to wrap my head around ToObjectSpace and welds.
Heres a gif representing what I want the parts to do with the White cube being Part1 and the Black cube being Part0:
This is my attempt at making this:
local Piston = script.Parent;
local weld = Instance.new("Weld");
weld.Parent = Piston;
weld.C0 = Piston.Part0.CFrame:Inverse();
weld.C1 = Piston.Part1.CFrame:Inverse();
weld.Part0 = Piston.Part0
weld.Part1 = Piston.Part1
local ts = game:GetService("TweenService");
local cfValue = Instance.new('CFrameValue')
cfValue.Value = weld.C0
local base = weld.C0
cfValue.Changed:Connect(function()
local travel = weld.Part0.CFrame + cfValue.Value.Position;
weld.C0 = travel:ToObjectSpace(weld.Part1.CFrame)
end)
local goal = {Value = CFrame.new(weld.Part0.CFrame.LookVector * 5)}
local cfTween = ts:Create(cfValue,TweenInfo.new(10),goal)
cfTween:Play()
wait(3)
ts:Create(weld.Part1, TweenInfo.new(4), {Position = weld.Part1.Position + Vector3.new(0,5,0)}):Play()
How would I recreate this in a script?