AngryTapok1337, I've fixed the code there, try it out. What error does the console report? Maybe the error appears in a different line. Or does it not display an error, but the function simply doesn't work?
Added 25 minutes later
I've tested it, and it works. However, there is one error: the destination point for delivering goods to the city should not be the location of the city itself, but the entrance point to the city, i.e., where heroes stand when they visit the city. Even without this fix, the caravan spawns and is filled with creatures. It just doesn't move and stays at the spawn point, daily displaying the standard error about the inability of caravans to move, as if it is blocked by something or the city garrison does not have available slots. Therefore, my initial script version (without the addition that checks for a completed quest) works.
It doesn't work at all, no matter how I insert it. At the same time, it doesn't display an error in the line where the caravan should spawn. It's as if there's nothing about the caravan in the code at all. Perhaps the problem lies in the rest of the code or in the map itself?