How do I make an object move from one point to another?

Hello everyone!! How are you?

So, the question is: I was doing a street, and wanted to program cars to be passing on this street infinitely. As I do not know how to program, I tried to start with something simple: try to program any object to move from point A to point B.

I watched several tutorials and ended up with the following code:

model = game.Workspace.group
start = model.A
end = model.B
position = model.object

while true
  position.Position = end.Position
  wait (2)
  position.Position = start.Position
  wait (2)
end

To help understand my variables:

Capture

But what happens when I run the code, is the object “teleporting” from point A to point B every 2 seconds hahahah xD

Can someone help me make this object go from point A to point B, instead of appearing and disappearing from one point to another?

Thank you!

16 Likes

Tweenservice is probably the best way you can go with what you’re looking for.

Since you want to be seeing that the car is moving from A to B, you can use Tweens to see this process happen then just seeing it jump from A to B. You’ll want it to transition from A to B.

		local info =  TweenInfo.new(
			1.5,
			Enum.EasingStyle.Sine,
			Enum.EasingDirection.InOut
			)
			local goals = {
				Position = B 
			}
		local Tween = qTween:Create(v,info,goals)
        Tween:Play()

If you want to be more efficient, you can do this tween on the client side rather than the server side which will make it jump significantly less.
You can implement that into your code by swapping it for when you change the position of your item.
Additionally, you should rename the variable position because you can get mixed up between the variable and the property itself.

Here is an example to what you’re looking for: https://www.youtube.com/watch?v=qgGDwTn0zgo

Hope you get to grips with scripting soon! :slight_smile:

13 Likes
tweenservice = game:GetService(“TweenService”)
model = game.Workspace.group
start = model.A
end = model.B
position = model.object

while true do
  position.Position = end.Position
  tweenservice:Create(position,TweenInfo.new(2,Enum.EasingStyle.Linear),{Position = start.Position}):Play()
  wait (2)
end

Edit: code format somehow cropped out the GetService line at the beginning.

14 Likes

Hello.
If you want to smoothly move the object from Position A to Position B use TweenService.

local TweenService = game:GetService("TweenService")
local Start = game.Workspace:WaitForChild("Start")
local End = game.Workspace:WaitForChild("End")

local PartToMove = game.Workspace:WaitForChild("PartToMove")
local TweenInformation = TweenInfo.new(
	0.5, -- Time
	Enum.EasingStyle.Linear, -- EasingStyle
	Enum.EasingDirection.InOut, -- Easing Direction
	5, -- Repeat Count
	true, -- Reverses to initial position? (true/false)
	1 -- Delay between each tween.
)
--[[ 
local EndProperties = {
	Position = Vector3.new(X,Y,Z)
} 

local EndProperties = {
	Position = End.Position
}

--]]
local EndProperties = {
	Position = End.Position	
}

local TweenToPlay = TweenService:Create(PartToMove,TweenInformation,EndProperties)

while true do
	TweenToPlay:Play()
	wait(6) -- After the Tween is finished it will stop for 6 seconds then replay.
end
11 Likes

The others have answered your question nicely. For future reference though, if you ever need to teleport something, please change Object.CFrame instead of Object.Position as the latter will not move to the exact position if another part is there.

Basically, let’s say there is a Part1 at [0, 0, 5]; you want to move Part2 from [0, 0, 0] to [0, 0, 5]. If you do it like this:

Part2.Position = Vector3.new(0, 0, 5); --Part2 will actually move to [0, 1, 5]

This is just a weird thing that Position does. To force Part2 to stay at [0, 0, 5] and not yield to Part1, you would change the CFrame value instead:

Part2.CFrame = CFrame.new(0, 0, 5); -- Perfect! But...

The only “caveat” is the rotational properties will be reset. You can fix this by doing:

Part2.CFrame = CFrame.new(0, 0, 5) * (Part2.CFrame - Part2.CFrame.p); -- Perfect! Position changed while rotation remains the same!

Sorry if I complicated this for you. If you have any questions, feel free to ask!

Also, the Dev Wiki has two great articles on CFrames:

10 Likes