Quickly getting undo/redo in command bar scripts

Command bar scripts do not support undo/redo by default. In order to use undo/redo in a safe and correct way, the command needs to have lots of extra code using ChangeHistoryService:

local chs = game:GetService("ChangeHistoryService")
local id = chs:TryBeginRecording(game:GetService("HttpService"):GenerateGUID())
if id then
   local success, result = pcall(function()
      -- your actual command code here
      -- return false if the operation should be cancelled for other reasons
      return true
   end)
   local op = if success and result then
      Enum.FinishRecordingOperation.Commit else
      Enum.FinishRecordingOperation.Cancel
   chs:FinishRecording(id, op)
   if not success then
      warn(("Command bar script failed:\n%s"):format(tostring(result)))
   end
end

This is really annoying, especially for small random commands. It would be nice if there was a very quick and easy way for us to get our command bar scripts safely running with undo/redo.

2 Likes