local Z=""local q=4 local C=4 local A=q*C local G=256 local k=256 local u=15 assert((numImages*q)*C>=u)local c=1 assert(c>0)local b=0 local W=0 local E=u/c local v=0 local function z(Z)v=math.fmod(v+Z,c)W=math.floor(v*E)local q=b b=math.floor(W/A)if q~=b then imageInstance.Image=imageAssetIDs[b+1]end local u=W-b*A local z=math.floor(u/C)local o=u-z*C imageInstance.ImageRectOffset=Vector2.new(o*G,z*k)end Animator.Connection=nil Animator.Init=function()Animator.Connection=(game:GetService("\082\117\110\083\101\114\118\105\099\101")).RenderStepped:Connect(z)end Animator.Stop=function()if Animator.Connection then imageInstance.Image=Z Animator.Connection:Disconnect()end end return Animator
-- Placeholder texture can be any roblox asset link
local initialTexture = ""

local RowCount = 4
local ColCount = 4
local perImageSpriteCount = RowCount * ColCount
local width = 256
local height = 256

local framesInTotal = 15
assert(numImages * RowCount * ColCount >= framesInTotal)

local animPeriod = 1 -- period for the whole animation, in seconds
assert(animPeriod > 0)

local curImageIdx = 0
local curSpriteIdx = 0

local fps = framesInTotal / animPeriod
local accumulatedTime = 0

local function AnimateImageSpriteSheet(dt)
	accumulatedTime = math.fmod(accumulatedTime + dt, animPeriod)
	curSpriteIdx = math.floor(accumulatedTime * fps)

	local prevImageIndex = curImageIdx
	curImageIdx = math.floor(curSpriteIdx / perImageSpriteCount)
	if prevImageIndex ~= curImageIdx then
		imageInstance.Image = imageAssetIDs[curImageIdx + 1]
	end

	local localSpriteIndex = curSpriteIdx - curImageIdx * perImageSpriteCount
	local row = math.floor(localSpriteIndex / ColCount)
	local col = localSpriteIndex - row * ColCount
	imageInstance.ImageRectOffset = Vector2.new(col * width, row * height)
end

Animator.Connection = nil

Animator.Init = function()
	Animator.Connection = game:GetService("RunService").RenderStepped:Connect(AnimateImageSpriteSheet)
end

Animator.Stop = function()
	if Animator.Connection then
		imageInstance.Image = initialTexture
		Animator.Connection:Disconnect()
	end
end

return Animator
