How to change speed of a tween?

What I want to achieve here is simple: Multiple tweens that span over several scripts, those of which can have their speed changed by simply changing a number value.

So far, I’ve discovered that by disabling tween scripts, changing the number value which controls the speed, then reenabling the scripts changes the speed. I’ve written a script to do this automatically, but the problem is that the tween doesn’t continue from where it left off when it’s disabled to change the value, and moves as if the position it was in when it was disabled was the starting position.

I’m asking for help to see if theres an easier roundabout way of doing this, or if theres a way to solve the issue im currently having.

Any help would be appreciated. Thanks.

4 Likes

Hey there! I don’t really understand what you are saying. Could you try to explain it better?

1 Like

I’m not to completely sure what you’re asking for here.

1 Like

Wait… are you asking for a system (apparently you have one so can you show it) that stops a tween, changes it’s speed, and then continues the tween from that point? I’m pretty sure you can just cancel the tween instead of stopping it.

1 Like

Thats what I’m saying, but instead of stopping the tween it disables the script the tween belongs to.

Lets say we have a part, which is being tweened from point A to point B. While the part is in the middle of moving towards point B, I disable the script, change a value, and then reenable it. The problem I’m currently having is that once I reenable the script, the part’s tween gets “shifted” and moves past point B as it had just started moving from point A.
Hope that clarifies it.

As far as I know, you can’t change the speed of a tween while it’s being ran, only before or after it has played.

I used to use a system like this a few years ago. All I did was cancel the current tween and create a new one. Canceling a tween does not reset the properties that were manipulated (in our case: the position). This allows you to just create a tween from the current position and have it travel towards point B with the new speed. I’ll give an example:

local TweenService = game:GetService("TweenService")
local PointA = YourPointHere -- Point A
local PointB = YourPointHere -- Point B
local PartToMove = YourPartHere -- Part to tween
local SpeedNum = YourNumberValue -- Number value
local CurrentTween -- Variable to store the current tween

-- Function to tween the part
local function TweenPart(Part, Speed, EndGoal)
   if (CurrentTween ~= nil and CurrentTween.PlaybackState ~= Enum.PlaybackState.Completed) then
      -- Cancel current tween
      CurrentTween:Cancel()
   end

   -- Create new tween
   CurrentTween = TweenService:Create(Part, TweenInfo.new(Speed, Enum.EasingStyle.Linear), {Position = EndGoal.Position})
   -- You can change the TweenInfo and EndGoal to a CFrame value if you wish, this is just for demonstration
   CurrentTween:Play() -- Play the tween
end

-- Initial movement
TweenPart(PartToMove, SpeedNum.Value, PointB)

-- Connect an event to detect when the number value is updated
SpeedNum:GetPropertyChangedSignal("Value"):Connect(function()
   -- Tween the part with the new value
   TweenPart(PartToMove, SpeedNum.Value, PointB)
end)

If you have any questions, let me know. Hope this helps :slightly_smiling_face:

What kind of value is SpeedNum meant to be? I tried using this script and the part didn’t move

The NumberValue that you mentioned in your original post. It’s supposed to be a number.

Can you send your script so I can see how you implemented it?

local model = script.Parent.Parent
local TweenService = game:GetService("TweenService")
local PointA = game.Workspace.pointa -- Point A
local PointB = game.Workspace.pointb -- Point B
local PartToMove = model.PrimaryPart -- Part to tween
local SpeedNum = game.ServerStorage.Speed -- Number value
local CurrentTween -- Variable to store the current tween

-- Function to tween the part
local function TweenPart(Part, Speed, EndGoal)
	if (CurrentTween ~= nil and CurrentTween.PlaybackState ~= Enum.PlaybackState.Completed) then
		-- Cancel current tween
		CurrentTween:Cancel()
	end

	-- Create new tween
	CurrentTween = TweenService:Create(Part, TweenInfo.new(Speed, Enum.EasingStyle.Linear), {Position = EndGoal.Position})
	-- You can change the TweenInfo and EndGoal to a CFrame value if you wish, this is just for demonstration
	CurrentTween:Play() -- Play the tween
	
	CurrentTween.Completed:Wait()
	
	model:SetPrimaryPartCFrame(CFrame.new(PartToMove.Position - Vector3.new(-2046, 0, 0)))
end

-- Initial movement
TweenPart(PartToMove, SpeedNum.Value, PointB)

-- Connect an event to detect when the number value is updated
SpeedNum:GetPropertyChangedSignal("Value"):Connect(function()
	-- Tween the part with the new value
	TweenPart(PartToMove, SpeedNum.Value, PointB)
end)

I simply copy pasted the whole thing to see if it would work. Should’ve also clarified that the part is actually a model, but in reality it’s still about moving the part since the script is located within the part and everything else in the model is welded to it.

Oh - that clears up everything. If it’s a model you should be setting the CFrame of the primary part rather than the position.

Just change the 3rd parameter in TweenService:Create to:

TweenService:Create(Part, UrTweenInfo, {CFrame = EndGoal.CFrame})

This should fix that issue. Also make sure that the parts welded to the primary part are unanchored (the primary part can be anchored).