Help on moving part script!

So I am trying to make a part move to anouther part! It should wait 20 secounds then move to location2!


Here is what it is in studio:
image

It should start at the the brown part which is connected to the grey part currently and go to the other part that’s a little far away but it doesn’t work here is my script:


local Location1 = script.Parent:WaitForChild("Location1");
local Location2 = script.Parent:WaitForChild("Location2");

local attach0 = Instance.new("Attachment");
attach0.CFrame = CFrame.new();
attach0.Parent = script.Parent;

local attach1 = Instance.new("Attachment");
attach1.CFrame = Location2.CFrame;
attach1.Parent = game.Workspace.Terrain;

local pris = Instance.new("PrismaticConstraint");
pris.ActuatorType = Enum.ActuatorType.Motor;
pris.MotorMaxForce = math.huge;
pris.Attachment0 = attach1;
pris.Attachment1 = attach0;
pris.Parent = script.Parent;


local forward = true;
pris.Velocity = SPEED;
attach1.CFrame = Location2.CFrame

Location1.Touched:Connect(function(hit)
	if (hit == script.Parent and not forward) then
		forward = true;
		pris.Velocity = SPEED;
		attach1.CFrame = Location2.CFrame
	end
end)

wait(20)

Location2.Touched:Connect(function(hit)
	if (hit == script.Parent and forward) then
		forward = false;
		pris.Velocity = -SPEED;
		attach1.CFrame = Location1.CFrame
	end
end)

I would suggest using TweenService to move the part.

local tInfo = TweenInfo.new(2, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut)
local goal = { CFrame = Location1.CFrame }
local tween = TweenService:Create(script.Parent, tInfo, goal)
tween:Play()
tween.Completed:Wait() -- comment this line out if you dont want the script to wait until the tween has finished
3 Likes

I think where you messed up is in the forward variable. Try instead of

Do
and not forward == true

Do the same for the false one

Try your Search button.
I just replied and fixed this person’s problem on this post a few days ago:

You don’t need to create the constraint with a script anymore, just use Studio.

I did see the game Platformer movement - Platformer movement - Roblox
and I could use the movepart code but I need it to wait before it moves to its destination
which I tryed using with wait(20) but then the script wouldn’t have any errors in the output and the part wouldn’t move I waited a long time to see but it never moved.

Attachments have a CFrame and a WorldCFrame. An attachment’s CFrame is the CFrame in object space, or a CFrame relative to whatever the attachment is attached to. WorldCFrame is in world space, and is likely what you’re trying to set it to. In other words, set the .WorldCFrame property and not the CFrame property.

not forward == true

and

not forward

Will behave exactly the same, not [condition] is just shorter than not [condition] == true

1 Like