Has Tween:Cancel() behaviour been changed?

Tween:Cancel() currently does not reset the tweened properties of the instance being tweened. Is this an intentional behaviour change or a bug?

To demo quickly try the example in here:

well :cancel() just stops the tween and leaves it where it was inside the tween. i dont think its a bug. but i could be wrong. i rather use a lerp sequence instead of tweenservice.

local start = CFrame.new(0,10,0)
local end = CFrame.new(0,0,0)

for i = 0,1,.1 do
part.CFrame = start:lerp(end,i)
wait()
end

here’s a video of mine explaining Difrences between lerp and tweenservice

2 Likes

The wiki page says that when a running tween is cancelled the part and the tweened values are reset to where they were once the tween started, but it doesn’t actually do this.

1 Like

The question now though is how long has this been broken? Did it ever revert?

I think if the behavior of it has always been to not revert the values, then it’s best the description of Cancel is changed rather than changing the behavior. Anyone who has used it in their game seriously has also tested, and may rely on this behavior already, having not known it’s apparently supposed to revert the values.

At the other end, this may be behavior people want, and if it’s something with some legitimate use cases I’m sure a new function could be added for this behavior. Though again, a lot of what I just said depends on how long (or if it’s ever) reverted values upon cancel.

1 Like

Yeah, it might be a good idea to make a feature request for something like Tween:Reset()

8 Likes

See, that sounds a lot more appropriate. ‘Cancel’ doesn’t even sound like it should be reverting values.

5 Likes

i think then its a typo of the wiki.

I’d be surprised if this wasn’t the behaviour at one point. After all there is a specific code sample on the wiki which assumes this.

Perhaps Tween:Stop() is your fix?

Looks like the Wiki info is a bit mislead. Cancel resets the tween variables, not the actual properties being tweened. Basically, if you call Cancel then Play in the middle of a tween, Play will tween to the remaining values using the entire time that was specified (Ex: you make a tween that is 5 seconds long. You call Cancel 2 seconds in. You call Play again. The tween will from this point still take 5 seconds). Pause does not do this, and does probably what you would expect when you call Play again.

Not sure how useful this behavior is, but it is there. I’ll update the wiki example to reflect this. Thanks for the report!

4 Likes

This caught my interest so I decided to extend the TweenService using metatables in order to extend the functionality of the Play function as well as adding a “Reset” function that not only cancels the tween and resets the tween variables but also resets the properties back to their original state. Feel free to use however you’d like. > OP w/ Example <

--[[
	Author: Acecateer
	Date Created: 2/27/18
--]]

local tweenService = game:GetService("TweenService")
local extendedService = {}

function extendedService:Create(instance, tweenInfo, props)
	local Tween = tweenService:Create(instance, tweenInfo, props)
	return setmetatable({}, {
		startProps = {},
		__mode = "kv", 
		__index	= function(obj, value)
			local raw_value = rawget(obj, value)
			if raw_value then
				return raw_value
			elseif value == "Play" then
				--[[
					Usage benefit:
					local Tween = tweenService:Create(...):Play()
					- Instead of -
					local Tween = tweenService:Create(...)
					Tween:Play()
				--]]
				return function(self, ...)
					local metaSelf = getmetatable(self)
					rawset(metaSelf, "startProps", {})
					local startProps = metaSelf.startProps
					for key, _ in next, props do
						if instance[key] then
							rawset(startProps, key, instance[key])
						end
					end
					Tween:Play()
					return self
				end
			elseif value == "Reset" then
				-- Reset properties back to original upon cancel
				return function(self, ...)
					Tween:Cancel()
					local startProps = getmetatable(self).startProps
					for key, value0 in next, startProps do
						if instance[key] then
							instance[key] = value0
						end
					end
				end
			else
				if (Tween[value] and type(Tween[value]) == "function") then
					return function(self, ...)
						return Tween[value](Tween, ...)
					end
				else
					return Tween[value]
				end
			end
		end
	})
end

return extendedService
3 Likes