Scripting error based on table.unpack()

This code is meant to loop through the table of values and insert them into the tween function in order for the primary part of a model to be moved.

I had originally done the animation in Moon Animator but further realized I couldn’t export CFrame, thus decided to script it.

My line of code “tween(0.5, table.unpack(frames[i], i))” keeps resulting in the error “attempt to call a Instance value”. It runs through the tween function once, then results in this error. Any advice?

local TS = game:GetService("TweenService")
local primPart = game.Workspace.Airplane.planeDoor.anchor

wait(2)
local function tween(timeNum, p1,p2,p3, o1,o2,o3)
	
	print("started")
	local tweenInfo = TweenInfo.new(timeNum,
		Enum.EasingStyle.Quad, -- change later
		Enum.EasingDirection.Out, -- change maybe too
		0,false,0)

	local targetpos = CFrame.new(p1,p2,p3)
	
	print(o1)
	
	local degToRads1 = o1 * math.pi / 180
	local degToRads2 = o2 * math.pi / 180
	local degToRads3 = o3 * math.pi / 180
										-- this is in RADIANS, not degrees!
	local goal = { CFrame = targetpos  * CFrame.fromEulerAnglesXYZ(degToRads1,degToRads2,degToRads3) }

	tween = TS:Create(primPart, tweenInfo, goal)

	tween:Play()
	print("ran")
end

local frames = {
	{69.801, 26.817, 48.355,		-2.384, 178.09, 0.079,},
	{70.3, 26.768, 49.526,			1.916, 36.578, 1.421},
	{70.3, 26.768, 49.526,			1.31, 56.699, 1.993},
	{70.3, 26.768, 49.526,			2.241, 20.016, 0.816},
	{70.3, 26.768, 49.526,			2.376, 4.952, 0.206},
	{70.3, 26.768, 49.526,			2.297, 15.632, 0.642},
	{70.3, 26.768, 49.526,			2.376, 4.952, 0.206},
	{70.3, 26.768, 49.526,			2.297, 15.632, 0.642},
	{70.3, 26.768, 49.526,			2.376, 4.952, 0.206},
	{70.3, 26.768, 49.526,			2.297, 15.632, 0.642},
	{70.3, 26.768, 49.526,			2.376, 4.952, 0.206},
	{70.3, 26.768, 49.526,			2.297, 15.632, 0.642},
	{70.3, 26.768, 49.526,			-55.732, 17.038, 7.241}
}

for i, v in pairs(frames) do
	wait()
	
	print(table.unpack(frames[i], i))
	tween(0.5, table.unpack(frames[i], i))

end

1 Like

tween is an Instance. Did you mean tween:Play()?
Also, please include the line number and as much other info as you can. It makes it far easier for us to track down what’s wrong.

1 Like

The error “attempt to call a Instance value” is likely caused by the fact that you are overwriting the tween function with the TweenService:Create() method call inside the tween() function. This means that after the first call to tween() , the tween variable is no longer referring to the tween() function but instead to the Tween instance returned by TweenService:Create() .

Try the following retyped function and let me know if it resolves your issue.

local function tween(timeNum, p1,p2,p3, o1,o2,o3)
    print("started")
    local tweenInfo = TweenInfo.new(timeNum,
        Enum.EasingStyle.Quad, -- change later
        Enum.EasingDirection.Out, -- change maybe too
        0,false,0)

    local targetpos = CFrame.new(p1,p2,p3)
    
    print(o1)
    
    local degToRads1 = o1 * math.pi / 180
    local degToRads2 = o2 * math.pi / 180
    local degToRads3 = o3 * math.pi / 180
                                        -- this is in RADIANS, not degrees!
    local goal = { CFrame = targetpos  * CFrame.fromEulerAnglesXYZ(degToRads1,degToRads2,degToRads3) }

    local tweenInstance = TS:Create(primPart, tweenInfo, goal)  -- rename the variable
    
    tweenInstance:Play()  -- use the renamed variable instead of 'tween'
    print("ran")
end

Hello,

Thank you for the responses, but coming back to this after not seeing it for about a week, I noticed my error. My issue was that I needed the tables inside the large table. I simply changed the for-loop at line 71 to

for i, v in pairs(frames) do
wait()

local localTable = frames[i]
print(table.unpack(localTable))
--print(table.unpack(frames[i], i))
tweenObj(0.5, table.unpack(localTable))

end

NOTE: Advice to others who may get stuck in the same situation, just script a new animation using Tween Service. It feels more efficient and simpler.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.