I'll try to keep this a concise as I can. Please bear with me.
I'm trying to work on an animation module that uses CFrame lerp
to move parts. Right now I'm just trying to get everything to work continuously in a single script before I move the code parts to the module.
All the data for the animations is in a table library
. I loop through each keyframe and interpolate the part according to the given data in the keyframe table inside the library. I have a separate module with easing functions that I also use for various easing styles, but that's not the main issue.
Animation table:
local animation1 = { -- For now the animation is with a single part keyFrame1 = { CoordinateFrame = CFrame.new(1, 0, 1) * CFrame.Angles(1, .3, .4), timeLength = 5, easingStyle = "inSine" }, keyFrame2 = { CoodrinateFrame = CFrame.new (10, 15, 1) * CFrame.new(0, 0, 0), timeLength = 5, easingStyle = "outSine" }, }
I have it so that it loops through the animation data in a continuous Heartbeat
event from RunService
that is shown below.
Animation Loop:
local keyFrame = 1 local v = animation1["keyFrame"..keyFrame] local i = 0 local t = v.timeLength local startingPartCFrame = part.CFrame -- Defines the starting CFrame for the Lerp. render = runService.Heartbeat:connect(function(step) local x = easing.inSine(i, 0, 1, 1) -- This is what calculates the fraction from 'i' to correctly interpolate the inSine. local newLerp = startingPartCFrame:Lerp(v.CoordinateFrame, x) -- ERROR HERE part.CFrame = newLerp i = i + (step/t) if i >= 1 and animation1["keyFrame"..keyFrame + 1] then print (true) -- It's after it runs through this block and moves to the next keyframe that I get the error keyFrame = keyFrame+1 v = animation1["keyFrame"..keyFrame] i = 0 x = 0 t = v.timeLength startingPartCFrame = part.CFrame newLerp = startingPartCFrame:Lerp(v.CoordinateFrame, x) elseif i >= 1 then render:Disconnect() end end)
Error:
The problem is when it loops to the second keyframe, and I get this error:
Argument 2 missing or nil
Script 'ServerScriptService.Script', Line 35
(Look in the script above for the error. I marked it with a comment "ERROR HERE")
I'm not sure what the cause of the problem is, so if anyone could please help it would be appreciated. Perhaps something with the StartingPartCFrame variable?
FULL SCRIPT
local runService = game:GetService("RunService") local part = game.Workspace.Part1 local easing = require(script.Parent:WaitForChild("MainModule")) -- Module for easing functions wait (3) local animation1 = { -- For now the animation is with a single part keyFrame1 = { CoordinateFrame = CFrame.new(1, 0, 1) * CFrame.Angles(1, .3, .4), timeLength = 5, easingStyle = "inSine" }, keyFrame2 = { CoodrinateFrame = CFrame.new (10, 15, 1) * CFrame.new(0, 0, 0), timeLength = 5, easingStyle = "outSine" }, } local keyFrame = 1 local v = animation1["keyFrame"..keyFrame] local i = 0 local t = v.timeLength local startingPartCFrame = part.CFrame render = runService.Heartbeat:connect(function(step) local x = easing.inSine(i, 0, 1, 1) local newLerp = startingPartCFrame:Lerp(v.CoordinateFrame, x) part.CFrame = newLerp i = i + (step/t) if i >= 1 and animation1["keyFrame"..keyFrame + 1] then print (true) -- It's after it runs through this block and moves to the next keyframe that I get the error keyFrame = keyFrame+1 v = animation1["keyFrame"..keyFrame] i = 0 x = 0 t = v.timeLength startingPartCFrame = part.CFrame newLerp = startingPartCFrame:Lerp(v.CoordinateFrame, x) elseif i >= 1 then render:Disconnect() end end)
In Sine function:
inSine = function(t, b, c, d) return -c * math.cos(t / d * (math.pi / 2)) + c + b end