How to move a part using RunService.Stepped?

Hi. What I need to do is move a part from position A to exactly position B using RunService.Stepped. This page of the API shows an example of how to move a part, but I want it to stop at exactly one target point. Please help me.

[Edit] I can’t use TweenService because I need to animate many parts at the same time and every so often I need to know the intermediate state of all the parts at the same time. Sorry for not clarifying this.

1 Like

Why not use TweenService?

local newCFrame = CFrame.new(20, 30, 40) -- whatever
local part = somePartSomewhere
local tweenInfo = TweenInfo.new(1, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut)
local tween = game:GetService("TweenService"):Create(part, tweenInfo , {CFrame = newCFrame})
tween:Play()
1 Like

Elaborate because we are unsure whether point A and point B changes or whether it is instantaneous or tweened movement.

In any case, you normally just set the CFrame or position of the object to a new one.

1 Like

I updated the question about why I can’t use tweenservice.

1 Like

Points A and B don’t change. It’s not an instantaneous movement (that’s why I use RunService). If I only check the CFrame I won’t get the exact target position.

1 Like

What do you mean by checking the CFrame? Of B or A?

1 Like

So you’d use Linear Interpolation

local RunService = game:GetService("RunService");


local function MovePart(A,B, Part, Time)
	local Start = tick();
	while tick() < Start + Time do
		Part.CFrame = CFrame.new(A:Lerp(B,(tick()-Start)/Time)) ;
		RunService.Stepped:Wait();
	end
	Part.CFrame = CFrame.new(B);
end


2 Likes

Consider using Linear Interpolation to help with this issue.

It’s not difficult to understand at all and fits an exact solution to your problem.

Argh! You beat me by a few seconds @VineyardVine :joy:

2 Likes
local function onStep(_, deltaTime)
	part.Position = part.Position + PART_SPEED * deltaTime
	if part.CFrame == CFrameTarget then
		-- disconnect stepped
	end
end

point B is the target.

VineyardVine and Kakaaaashi. This seems to be what I’m looking for. Thank you both.
I’ll pass and let you know what happens

1 Like

Incredible!, this really works. The answer is Linear Interpolation. Thank you very much.

I updated the code to use time rather than steps for more control