Creating metatables for animation

I’m creating a metatable for animating spritesheets. The part where I don’t know is how to animate them. I got the :Play() and :Pause() working

(I’m completely new to metatables)

1 Like

we need more information.

Can you show the spritesheet?
Can you show the script?

Spritesheet: (just bfdia fries doing the gangnam style, a placeholder)

One thing to note is that I did try to find a way how to do it but it didn’t work
Script:

--[[ MY PREVIOUS SCRIPT, TRYING TO CONVERT TO OBJECT ORIENTED PROGRAMMING (or whatever metatables are called)
There was an old function here, but it was deleted in an edit.
]]

local animation = {}
animation.__index = animation
local animationList = {}

function animation.new(imageDir, columns, rows, totalFrames, framerate, looped)
	local self = setmetatable({}, animation)
	self.State = false
	if typeof(imageDir) ~= 'Instance' then
		self.Directory = nil
	elseif (not imageDir:IsA('ImageLabel')) and (not imageDir:IsA('ImageButton')) then
		self.Directory = nil
	else
		self.Directory = imageDir
	end
	self.Columns = columns
	self.Rows = rows
	self.TotalFrames = totalFrames
	self.Framerate = framerate or 24
	self.CurrentFrame = 1
	self.CurrentRow = 1
	self.CurrentColumn = 1
	self.Looped = looped or false
	if table.find(self, nil) ~= nil then
		warn('Required values are nil!')
		return nil
	end
	table.insert(animationList, self)
	return self
end

function animation:Play()
	self.State = true
end

function animation:Pause()
	self.State = false
end

function animation:Stop()
	self.State = false
	self.CurrentFrame = 1
	self.CurrentRow = 1
	self.CurrentColumn = 1
end

function animation:Destroy()
	if table.find(animationList, self) ~= nil then
		table.remove(animationList, table.find(animationList, self))
		self.State = nil
		self.Directory = nil
		self.Columns = nil
		self.Rows = nil
		self.TotalFrames = nil
		self.Framerate = nil
		self.CurrentFrame = nil
	end
end

function animation.Prin()
	print(animationList)
end


game:GetService('RunService').Heartbeat:Connect(function(delta)
	for n, tabl in pairs(animationList) do
			if tabl.State == true then
				task.spawn(function()
					while true do
						if tabl.State == false then
							break
						end
						tabl.CurrentFrame += 1
						if tabl.CurrentFrame > tabl.TotalFrames then
							if tabl.Looped == true then
								tabl.CurrentFrame = 1
								tabl.CurrentColumn = 1
								tabl.CurrentRow = 1
							else
								break
							end
						end
						tabl.CurrentColumn = tabl.CurrentFrame-((tabl.CurrentRow-1)*tabl.Columns)
						if tabl.CurrentColumn > tabl.Columns then
							tabl.CurrentColumn = tabl.CurrentColumn - tabl.Columns
							tabl.CurrentRow += 1
						end
						--tabl.I = UDim2.fromScale(-(t-1), -(curRow-1))
						tabl.Directory.Position = UDim2.fromScale(-(tabl.CurrentColumn-1), -(tabl.CurrentRow-1))
						wait(1/tabl.Framerate)
					end
				end)
			end
	end
end)

return animation