Undo redo system is not working correctly. How can I fix this?

I have been trying for awhile on this system, the undo works as far as I know, but the redo I can’t get anything to work.


-- OOP boilerplate - create a class, add __index, then make a constructor
Stack = {}
Stack.__index = Stack
function Stack.new() return setmetatable({}, Stack) end

-- put a new object onto a stack
function Stack:push(input)
	self[#self+1] = input
end
-- take an object off a stack
function Stack:pop()
	assert(#self > 0, "Stack underflow")
	local output = self[#self]
	self[#self] = nil
	return output
end

local CacheTable = Stack.new()
local redoTable = Stack.new()

local function compactItemsForCache(child)
	if child then
		if child.Name ~= "UIStroke" and child.Name ~= "UICorner" then
			local tableOfItems = {}
			tableOfItems.obj = child
			tableOfItems.size = child.Size
			tableOfItems.pos = child.Position
			tableOfItems.rotation = child.Rotation
			tableOfItems.layer = child.ZIndex
			tableOfItems.color = child.BackgroundColor3
			tableOfItems.anchorPoint = child.AnchorPoint
			tableOfItems.borderSizePixel = child.BorderSizePixel

			--local newtable = compactedObjects[tableOfImportance]

			return tableOfItems
		end
	end
end

function cacheDrawing(ui)
	if #CacheTable >= 20 then
		CacheTable:pop()
		oldCacheNumber += 1
		local tableOfCache = {}
		for i, v in pairs(Canvas:GetChildren()) do
			if v.Name ~= "UICorner" and v.Name ~= "UIStroke" then
				local compactedChild = compactItemsForCache(v)
				table.insert(tableOfCache, compactedChild)
			end
		end
		CacheTable:push(tableOfCache)
		totalAmountOfCache += 1
	else
		totalAmountOfCache += 1
		local tableOfCache = {}
		for i, v in pairs(Canvas:GetChildren()) do
			if v.Name ~= "UICorner" and v.Name ~= "UIStroke" then
				local compactedChild = compactItemsForCache(v)
				table.insert(tableOfCache, compactedChild)
			end
			
		end
		CacheTable:push(tableOfCache)
	end
end

function UndoAction(index)
	if #CacheTable ~= 0 then
		local getframes = CacheTable[#CacheTable - 1]
		print(getframes[1])
		CacheTable:pop()
		redoTable:push(Canvas:GetChildren())

		for i, v in pairs(Canvas:GetChildren()) do
			if v.Name ~= "UICorner" and v.Name ~= "UIStroke" then
				v:Destroy()
			end
		end

		for x, c in pairs(getframes) do
			print(c)
			print(c["pos"])
			local newFrame = Instance.new("Frame")
			newFrame.Position = c["pos"]
			newFrame.Size = c["size"]
			newFrame.Rotation = c["rotation"]
			newFrame.BackgroundColor3 = c["color"]
			newFrame.AnchorPoint = Vector2.new(0.5,0.5)
			newFrame.BorderSizePixel = c["borderSizePixel"]
			local UICorner = Instance.new("UICorner")
			UICorner.CornerRadius = UDim.new(1,0)
			UICorner.Parent = newFrame
			newFrame.Parent = Canvas
			print(newFrame)
		end
	end
end

function RedoAction(index)
	if #redoTable ~= 0 then
		local getframes = redoTable[#redoTable]
		CacheTable:push(redoTable:pop())
		--[[table.insert(redoTable, Canvas:GetChildren())
		redoTable:push(Canvas:GetChildren())]]

		for i, v in pairs(Canvas:GetChildren()) do
			if v.Name ~= "UICorner" and v.Name ~= "UIStroke" then
				v:Destroy()
			end
		end

		for x, c in pairs(getframes) do
			print(c)
			print(c["pos"])
			local newFrame = Instance.new("Frame")
			newFrame.Position = c["pos"]
			newFrame.Size = c["size"]
			newFrame.Rotation = c["rotation"]
			newFrame.BackgroundColor3 = c["color"]
			newFrame.AnchorPoint = Vector2.new(0.5,0.5)
			newFrame.BorderSizePixel = c["borderSizePixel"]
			local UICorner = Instance.new("UICorner")
			UICorner.CornerRadius = UDim.new(1,0)
			UICorner.Parent = newFrame
			newFrame.Parent = Canvas
			print(newFrame)
		end
	end
end

Any help and improvements would be appreciated

2 Likes