Can't figure out TweenPosition error

I’ll get straight to the point: I’m getting an error while using TweenPosition and I have no clue why.

local TweenSettings = {
    Enum.EasingDirection.Out,
    Enum.EasingStyle.Bounce,
    .5,
    true
}

TextLabel:TweenPosition( --error is thrown from this line
    UDim2.new(1, 2, 3, 4),
    unpack(TweenSettings), --if I put in the actual table values and remove the unpack, it works
    function() TextLabel.Text="" end --if I don't put in this line, it works
)

With the above code, I get:
image

Hope someone smarter than me can help me out. Thanks in advance.

1 Like

That’s a weird way to use unpack and TweenPosition. I personally would avoid using these weird methods in the first place, as TweenService can accomplish what you require better than what these can.

What I assume is happening here is that unpack is returning only one argument based on your usage. Unpack would suggest varargs which is only valid when attached to the end of a function’s parameters. It doesn’t push all the returns like you’d expect it to. This is based on your comment of how unpack is the problem segment and removing it for hard-coded parameter assignment fixes the issue, as well as how the function is being assigned to an incorrect slot.

1 Like

The issue is that you’re passing the function after the unpack.
i.e
If you pass anything after a tuple, that tuple will be forced to be a length of 1

(function(...)
    print(select("#",...))
    --> 3
    print(select("#",...,nil))
    --> 2
end)(1,2,3)

Either include the function in the tween setting or get each component individually.

local TweenSettings = {
    Enum.EasingDirection.Out,
    Enum.EasingStyle.Bounce,
    .5,
    true
}

TextLabel:TweenPosition(
    UDim2.new(1, 2, 3, 4),
    TweenSettings[1],TweenSettings[2],TweenSettings[3],TweenSettings[4],
    function() TextLabel.Text="" end
)
1 Like

I get that I can do it using alternative methods, but I’d still like to know why this method doesn’t work.

I’m a table and then unpacking it because I don’t feel like copypasting my tween settings every single time I want to tween something; my main UI handler puts the settings into shared and then all my UI uses these settings, which can be easily changed in one place.

I’ve tested unpack when attempting to determine the problem and it worked as intended, and I’ve also used it in the past without incident. It’s also not just for varargs, it’s designed to work with any table.

Didn’t know that was a thing, thank you!

Edit: I’ve settled for this:

local TweenSettings = setmetatable({
    Enum.EasingDirection.Out,
    Enum.EasingStyle.Bounce,
    .5,
    true
}, {__call=function(tbl, func)
	local CustomSettings = {unpack(tbl)}
	table.insert(CustomSettings, func)
	return CustomSettings
end})

The unpack is still valid. It’s where you put it that matters. I did mention this in my post:

Hala explained the rest of it where my uncertainty lay and provided an explanation of the behaviour behind it, so refer to his reply for information.