How to update a Tween while its playing?

This is the relevant code in question.

local TweenService = game:GetService('TweenService')
local TSTween = TweenService:Create(Player.PlayerGui.STWBar.BarVal, TweenInfo.new(Player.PlayerGui.STWBar.BarVal.Value/100), {Value = 0}) --Tween. 
TSTween:Play()

The tween works fine, but if “STWBar.BarVal.Value” were to decrease/increase, for example like this:

player.PlayerGui.STWBar.BarVal.Value = player.PlayerGui.STWBar.BarVal.Value + 100

The tween won’t update to show the change in the value. Is there any way I can get the Tween to show changes happening to the “STWBar.BarVal.Value” while the Tween is playing?

(Player.PlayerGui.STWBar.BarVal is just the path the IntValue, so Player.PlayerGui.STWBar.BarVal.Value is just getting the value of said IntValue.)

I would stop the tween, update the goal recreate the tween and, replay the tween

Tween:Stop()
goal.Position = NewPos
local Tween = TweenService:Create(Part,Info,goal)
Tween:Play()

Won’t be able to use that. The script that handles the Tweening and the script that changes/modifies the “STWBar.BarVal.Value” are 2 different scripts. Maybe if I used a listener event to tell the script that handles the Tweening when the script that changes/modifies the “STWBar.BarVal.Value” has changed the value.

Bumping because issue still hasn’t been resolved.

Playing a new tween while the old tween is playing should override it without having to do anything else.

You mean like this?

local TSTween = TweenService:Create(Player.PlayerGui.STWBar.BarVal, TweenInfo.new(Player.PlayerGui.STWBar.BarVal.Value/100), {Value = 0})
TSTween:Play()
wait(5)
TSTween:Play()

I’m assuming this is what you meant by “Playing a new tween while the old tween is playing”.

2nd bump(issue still hasn’t been resolved).

yep, i tried it, playing another tween will override the current tween.

local TSTween = TweenService:Create(Player.PlayerGui.STWBar.BarVal, TweenInfo.new(Player.PlayerGui.STWBar.BarVal.Value/100), {Value = 0})
TSTween:Play()
wait(5)
-- Redefine the TSTween so it will tween to the newest values
TSTween = TweenService:Create(Player.PlayerGui.STWBar.BarVal, TweenInfo.new(Player.PlayerGui.STWBar.BarVal.Value/100), {Value = 0})
TSTween:Play()

or another alternative

-- create a new object and destroys the old one
local newObject = Player.PlayerGui.STWBar.BarVal:Clone()
newObject.Parent = Player.PlayerGui.STWBar
newObject.Value = Player.PlayerGui.STWBar.BarVal.Value
Player.PlayerGui.STWBar.BarVal:Destroy()
local TSTween = TweenService:Create(Player.PlayerGui.STWBar.BarVal, TweenInfo.new(Player.PlayerGui.STWBar.BarVal.Value/100), {Value = 0})
TSTween:Play()

I just tried that but if affects the tween time if you do it too much.

I.E

coroutine.resume(coroutine.create(function()
		local TSTween = TweenService:Create(Player.PlayerGui.STWBar.BarVal, TweenInfo.new(Player.PlayerGui.STWBar.BarVal.Value/100), {Value = 0})
		TSTween:Play()
		wait(0.1)
		TSTween = TweenService:Create(Player.PlayerGui.STWBar.BarVal, TweenInfo.new(Player.PlayerGui.STWBar.BarVal.Value/100), {Value = 0})
		TSTween:Play()
			wait(0.1)
			TSTween = TweenService:Create(Player.PlayerGui.STWBar.BarVal, TweenInfo.new(Player.PlayerGui.STWBar.BarVal.Value/100), {Value = 0})
			TSTween:Play()
			wait(0.1)	
			TSTween = TweenService:Create(Player.PlayerGui.STWBar.BarVal, TweenInfo.new(Player.PlayerGui.STWBar.BarVal.Value/100), {Value = 0})
			TSTween:Play()
			wait(0.1)	
			TSTween = TweenService:Create(Player.PlayerGui.STWBar.BarVal, TweenInfo.new(Player.PlayerGui.STWBar.BarVal.Value/100), {Value = 0})
			TSTween:Play()
			wait(0.1)	
			TSTween = TweenService:Create(Player.PlayerGui.STWBar.BarVal, TweenInfo.new(Player.PlayerGui.STWBar.BarVal.Value/100), {Value = 0})
			TSTween:Play()
			wait(0.1)	
			TSTween = TweenService:Create(Player.PlayerGui.STWBar.BarVal, TweenInfo.new(Player.PlayerGui.STWBar.BarVal.Value/100), {Value = 0})
			TSTween:Play()
			wait(0.1)	
			TSTween = TweenService:Create(Player.PlayerGui.STWBar.BarVal, TweenInfo.new(Player.PlayerGui.STWBar.BarVal.Value/100), {Value = 0})
			TSTween:Play()
			wait(0.1)	
			TSTween = TweenService:Create(Player.PlayerGui.STWBar.BarVal, TweenInfo.new(Player.PlayerGui.STWBar.BarVal.Value/100), {Value = 0})
			TSTween:Play()
			wait(0.1)	
			TSTween = TweenService:Create(Player.PlayerGui.STWBar.BarVal, TweenInfo.new(Player.PlayerGui.STWBar.BarVal.Value/100), {Value = 0})
			TSTween:Play()

Not a good fix in the long run.

3rd bump(issue still hasn’t been resolved).

4th bump?(issue hasn’t been resolved).

maybe this?

local TSTween
while true do
	TSTween = TweenService:Create(Player.PlayerGui.STWBar.BarVal, TweenInfo.new(Player.PlayerGui.STWBar.BarVal.Value/100), {Value = 0})
	TSTween:Play()
	wait()
end

or

while true do
	Player.PlayerGui.STWBar.BarVal.Value -= 1
	wait()
end

This has the same issue as eyrtuiop232’s post. If you tween too much, it shortens the time of the tween. I’ll provide a video of this in a second to show you.

do you want a constant speed like if the value is 40 or 50 or anything it still takes 1 second to go to 0?

No, the time of the tween depends on the amount of “bar” there is. The bar can reach up to a thousand, so when you see “Player.PlayerGui.STWBar.BarVal.Value/100”, its turning the bar into “seconds”. For example, if the bar is 500, it’ll do 500/100, making it tween the bar to 0 in 5 seconds.

This is the normal tween, without adding anything on it.
Relevant code for reference:

local TweenService = game:GetService('TweenService')
local TSTween = TweenService:Create(Player.PlayerGui.STWBar.BarVal, TweenInfo.new(Player.PlayerGui.STWBar.BarVal.Value/100), {Value = 0}) --Tween. 
TSTween:Play()

If you were to loop the code above, it’d allow the Tween the update, but it’d also shorten the tween time, which is not optimal for me.

Found the “fix” on my own. Not really a fix just a workaround. Changed how the bar values are modified, so you have to fire an event to change the amount. The script that handles the tweening listens for when the event is fire, and if it ever fires during the tween duration, the tween cancels and restarts. This allows for the values to change mid tween without shorterning tween time.

local TSTween = TweenService:Create(Player.PlayerGui.STWBar.BarVal, TweenInfo.new(Player.PlayerGui.STWBar.BarVal.Value/100), {Value = 0})
		TSTween:Play()
		
		local BEC = game.ReplicatedStorage.BarEvent.Event:Connect(function()
			TSTween:Cancel()
			TSTween = TweenService:Create(Player.PlayerGui.STWBar.BarVal, TweenInfo.new(Player.PlayerGui.STWBar.BarVal.Value/100), {Value = 0})
			TSTween:Play()
			print("fired")
		end)

Code that fires whenever a bar valued is change:

game.ReplicatedStorage.BarEvent:Fire(player, -100) --lowering bar value
game.ReplicatedStorage.BarEvent:Fire(player, 100) --increasing bar value

(Event used is a BindableEvent).

1 Like