How can I tween a part using speed rather than time?

If I want to tween a part I can do it like this:

local Time = 10
local TweenInfo = TweenInfo.new(
		Time,
		
		Enum.EasingStyle.Linear,
		
		Enum.EasingDirection.Out,
		
		0,
		
		false,
		
		0
	)
	
	TweenService:Create(Part, TweenInfo, {CFrame = CFrame.new(0, 10, 0)}):Play()

But to tween something, you have to tell the TweenInfo how long it will take for the part to get to that location. How can I tween a part based on how fast I want it to go rather than how long it should take?

8 Likes

How fast you want it, it’s the speed you give to it, so, if you change the ‘Time’ to 0.1, it will go really fast, else, if you change time to, 100, it will go really slow. I think that’s what you mean, else if you’re doing something like bullets, you can just do a loop to move it absolubtly fast, and make it goes down like in real life right, is that what you mean?

4 Likes

I know that, but how would I detect how long it should take based on how fast the part is?

local speed = 40 -- This is how fast the part is.
2 Likes

Oh like getting velocity of the part right, I do not know if tweenservice change velocity which I don’t think so, because it’s changing CFrame, probably somethin like diving the time and for the new cframe, I really do not know, not really experienced with this stuff as dividing, kind of new or / * ^ % and more, lol

2 Likes

Well, velocity is distance divided by time.

1 Like

Well, atleast I was close :laughing:

2 Likes

Yeah, one forgets this kind of stuff. Even I, someone who loves physics forgot the formula lol.

4 Likes

It’s something you’ll have to solve for manually based on what you’re tweening.
Basically, Speed = Distance / Time. If you want to solve for time, you’ll be calculating Distance/Speed.
In the case of tweening a part’s position, if you have a starting point and a destination point both represented by Vector3s, you can subtract the destination point by the starting point, then take the magnitude of the resulting vector to get the distance. If you divide the magnitude of the resulting vector by the speed, you’ll get the time it should take to cover that distance for that speed.

5 Likes

How would I get the speed if Time isn’t solved?

How would I solve for time, if speed isn’t solved?

Unless speed is just a variable local speed = 40 -- It can be any number really?

1 Like

In your post, it says that you want to tween based on speed, so do
"((TargetPos - StartingPos).Magnitude)/speed)

5 Likes

Yeah, speed would be defined by you in a local variable. All that matters is that speed must be positive. Naturally, higher speed means a faster tween. If speed wasn’t given, you wouldn’t be able to solve for time.

2 Likes

I’ve had to work on this for my own game. Here’s basically the formula being put into action.


local Speed = 16
function GetTime(Distance, Speed)
	-- Time = Distance / Speed
	local Time = Distance / Speed
	return Time
end

local NewPoint = Points:WaitForChild(""..i) -- New selected point/node
local Distance = (Model.PrimaryPart.Position - NewPoint.Position).magnitude -- Get the distance between the current position and the next node
local Time = GetTime(Distance, Speed) -- Calculate the time
print(Time)
		
local NewTweenInfoTable = TweenInfo.new(
	Time, -- Time
	Enum.EasingStyle.Linear,
	Enum.EasingDirection.Out, 
	0, -- RepeatCount (-1 = Infinite)
	false, -- Reverse tween after finishing
	0 -- DelayTime
)
-- Then you would make the Tween with the TweenInfo
27 Likes