Make the speed of a Tween change when a number value is changed?

You can write your topic however you want, but you need to answer these questions:

Hi, I need the speed value of a Tween to change when a separate number value is changed. I am relatively new to scripting so bare with me here but I have tried a few different things and none of them have seemed to work. One of these methods was creating a separate tweenInformation variable for when a value changed and used a while loop which would continue to loop until the value changed, then another loop would begin with a different speed. This was problematic because when the previous tween got canceled, if the tween were to stop in a halfway position, when the other tween kicked in, it would go to the position I wanted it to but then stop at the halfway point and repeat.

Here’s the script that I tried to make so that a separate variable could control the speed.
Horrible script that I wrote:

local PumpValue = game.Workspace.CORESystems.CoolingValues.Pump1Value
local tweenService = game:GetService("TweenService")
local Part = script.Parent
local Speed = game.Workspace.CORESystems.CoolantPumps.CoolantPump1.MovingPartSpeeds
local tweeningInformation = TweenInfo.new(

	Speed.Value, -- How long will the animation take?
	Enum.EasingStyle.Sine, -- Animation
	Enum.EasingDirection.InOut, --Easing Direction
	1, -- How many times will it repeat?
	true, -- Will it repeat?
	0 -- Repeat delay
)

local partProperties = {

	Position = Vector3.new (59.61, 18.63, 5.702)
}


local Tween = tweenService:Create(Part,tweeningInformation,partProperties)

while true do
	wait()
	Tween:Play()
	end

Thank you so much for trying to help. I apologies if this made absolutely zero sense.

Apparently you have to overwrite Tween with a new Tween with new TweenInfo every time you fire, there’s no way you can change the existing ones.

1 Like

This is good info, however, I still have a problem, as when I make a new tweenInfo and it fires when the value changes, if the previous tween stops halfway, the new tween will pick up at the halfway point, go to the end position and then go back to the halfway point and repeat. Thanks for the help. Any ideas on how to fix this?

Might just be easier to use a for loop rather than messing around with Tweens.

EDIT: I mean while loop

Hey, sorry to bother… but I am extremely new to scripting and I’m a little confused as to how I could make this part move via a for loop. Could you explain a little? Thank you so much!

Actually, before you even create the Tween, allow the script to reposition the object, or let the previous tween finish by using Tween.Completed:Wait().

I was thinking something like this:

local Speed = 0.05

while wait(Speed) do
	script.Parent.Position += Vector3.new(0.1, 0, 0)
end

You should then be able to make it faster by lowering the ā€˜speed’ and slower by raising it. The only downside to this is the movement is less smooth than with Tweens.

1 Like

This is a good idea and I tried it, but it has some issues. The way this is working is there is six buttons changing the speed of the object. So, If someone presses say two and the first one is not running, the Tween.Completed.Wait() does not seem to be working. Otherwise, this is a good idea. I think I will be going with @r_apt 's response of just bypassing the Tween completely, though it isn’t exactly what I would have liked. Thanks for your help!

This is what I am going to have to be going with. It may cause a bit of lag and wouldn’t be my first choice, but it will keep the script more organized and work better in general. Thanks for your help!

1 Like

You could try something like this:

local PumpValue = game.Workspace.CORESystems.CoolingValues.Pump1Value
local tweenService = game:GetService("TweenService")
local Part = script.Parent
local Speed = game.Workspace.CORESystems.CoolantPumps.CoolantPump1.MovingPartSpeeds

local forward_tween, backward_tween do

	local tween_info = TweenInfo.new(Speed.Value/2,Enum.EasingStyle.Sine,Enum.EasingDirection.InOut)

	local start_point = {
		Position = Part.Position
	}
	local end_point = {
		Position = Vector3.new(59.61, 18.63, 5.702)
	}
	
	local function define_tweens()
		backward_tween = tweenService:Create(Part,tween_info,start_point)
		forward_tween = tweenService:Create(Part,tween_info,end_point)
	end
	define_tweens()
	
	Speed.Changed:Connect(function(new_duration)
		tween_info = TweenInfo.new(new_duration/2,Enum.EasingStyle.Sine,Enum.EasingDirection.InOut)
		define_tweens()
	end)
end

while true do
	forward_tween:Play()
	forward_tween.Completed:Wait()
	backward_tween:Play()
	backward_tween.Completed:Wait()
end

This will allow you to continue using Tweens. The speed won’t change mid-cycle, but it should update once the currently-executing tween finishes. By breaking the tween up into start and stop tweens, you get around the issues around repeating tweens ā€œrestartingā€ from the wrong position.

1 Like

This is actually super smart, too… Ill be trying it out as well. I’ll see what works best.