System and Storage functions cause a crash in a public game

When working in the development environment the following code that uses the system and storage functions works. But once the game is published, when that same code is triggered, the game stalls out and the window eventually crashes. Is there a better way to call the system and storage functions for actual gameplay, instead of in the development window?

note I've updated the second code block as a workaround. It now will load a blank object instead of the files in the 'sprites/background' folder, if it stalls out.

save_game = function()
  storage.set("saved game", "gamefile")
  storage.set("player", player) 
  storage.set("level", level_index)
end
indir = "lvl1"

files = system.project.listFiles( "sprites/background/"+indir, function( list, error) print(list) end )
  while debug_counter < 1000 
    if files.ready then
      background_layers = files.list
      loading = false
      debug_counter = 0
      break
    
    else
      print('trying to load background!' + debug_counter)
      loading = true
      debug_counter += 1
      sleep(3)
    end
  end