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:
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
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.
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.
Yeah, it might be a good idea to make a feature request for something like Tween:Reset()
See, that sounds a lot more appropriate. āCancelā doesnāt even sound like it should be reverting values.
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!
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