CC: Tweaked · Troubleshooting · Intermediate
CC: Tweaked Troubleshooting Guide: Fixing 'Too Long Without Yielding' & Turtle Resets
Writing complex automation scripts in CC: Tweaked often leads to frustrating roadblocks like crashed programs, wiped Turtle inventories, or unresponsive computers. This guide teaches you how to diagnose and permanently fix the most notorious ComputerCraft errors, including yielding crashes, nil indexing, infinite startup loops, and state loss during chunk unloads. Mastering these debugging and persistence techniques will transform your fragile prototypes into rock-solid, server-friendly infrastructure.
What you need
- Computer or Advanced Computer
- Turtle or Advanced Turtle (such as a Mining Turtle)
- Disk Drive
- Floppy Disk
- Wired Modem and Networking Cable (optional, for peripheral testing)
- Basic familiarity with the CraftOS terminal and the built-in 'edit' program
Step by step
1Fix 'Too Long Without Yielding' Crashes
When your script crashes with the message 'Too long without yielding', it means you have written a heavy loop (typically a 'while true do' loop) that never pauses to let other computers run. Open your script using 'edit <filename>' and locate any continuous loop that lacks a pausing function. Insert 'os.sleep(0.05)' or 'os.pullEvent()' inside the loop body. This forces the script to pause for at least one game tick, satisfying the watchdog timer and preventing the crash.
Tip: If you need a high-speed computation loop that cannot afford to wait a full game tick, queue a dummy event with 'os.queueEvent("yield")' immediately followed by 'os.pullEvent("yield")' to yield instantaneously.
2Resolve 'Attempt to Index Nil' Errors
The error 'attempt to index ? (a nil value)' happens when your code tries to call a function on a table or peripheral that does not exist. Note the line number reported in the red error text and open your script to that line. If you are wrapping a peripheral like a Monitor or Chest using 'peripheral.wrap("top")', verify in-game that the block is physically attached to that exact side, or that the Wired Modem attached to it is right-clicked and glowing red. Add a safety check in your code right after wrapping, such as 'if not myPeripheral then error("Peripheral missing!") end', to catch hardware disconnects cleanly.
Tip: Use 'peripheral.find("monitor")' instead of hardcoding directional sides so your script automatically locates the hardware even if you move it.
3Break Out of Infinite Startup Loops
If you put a faulty loop inside a 'startup.lua' file and block termination with 'os.pullEvent = os.pullEventRaw', Ctrl+T will no longer rescue you, leaving the Computer bricked. To fix this without destroying the block, place a Disk Drive directly adjacent to the locked Computer. Craft a Floppy Disk, insert it into a working computer's Disk Drive, and type 'edit disk/startup.lua' to create a completely blank file, then save and exit. Move this Floppy Disk into the Disk Drive next to your locked Computer, open the Computer's GUI, and hold Ctrl+R to reboot; it will prioritize the blank disk startup over its internal broken startup, dropping you safely into the command prompt where you can type 'rm startup.lua'.
Tip: Always keep an emergency 'Rescue Floppy' with a blank startup file inside your main tool bag.
4Prevent Data Loss When Breaking Turtles
By default, picking up a Computer or Turtle with a pickaxe completely wipes its filesystem and fuel level when you place it back down. Before deploying any Turtle into the world, open its terminal and run the command 'label set <name>' (for example, 'label set QuarryBot1'). Once a Computer or Turtle has an assigned label, Minecraft binds its internal ID, hard drive contents, and internal fuel buffer to the dropped item. You can now safely break, move, and replace the Turtle without losing a single line of code.
Tip: Check the item tooltip in your inventory or JEI; a properly labeled Turtle will display its custom name and remaining fuel right on the item card.
5Save Turtle State to Survive Chunk Unloads
Even with a label, whenever a chunk unloads or the server restarts, a running Turtle instantly powers off and forgets what line of code it was executing. To make a Turtle resume its job automatically, you must save its current progress to a file after every single action. In your script, create a state table (such as 'local state = {x=0, z=0, step="mining"}') and write a helper function that uses 'fs.open("state.txt", "w")' along with 'textutils.serialize(state)' to save that table to the hard drive right before and after every 'turtle.forward()' or 'turtle.dig()' call.
Tip: File writes are fast, but avoid writing to disk inside pure math loops; only write to disk when the Turtle actually changes its physical position or inventory state.
6Build an Auto-Resuming Startup Script
Now that your Turtle logs its progress to 'state.txt', you must program it to read that file when the chunk reloads. Create a 'startup.lua' file on the Turtle that checks if 'fs.exists("state.txt")' is true. If the file exists, open it in read mode ('fs.open("state.txt", "r")'), convert the text back into a Lua table using 'textutils.unserialize(file.readAll())', and pass those saved coordinates into your main work function. When the server restarts or you walk back into the chunk, the Turtle will boot up, read its last recorded action, and seamlessly continue where it left off.
Tip: remember to delete 'state.txt' using 'fs.delete("state.txt")' once the Turtle finishes its entire job so it does not try to resume a completed task on the next reboot.
7Synchronize Physical Movement with State Logs
A tricky edge case occurs if a server crashes in the exact millisecond between a Turtle moving forward and writing its new coordinates to the hard drive. To prevent coordinate drift upon restart, either connect your Turtle to a local GPS network using a Wireless Modem and 'gps.locate()', or record the intent to move before moving. By saving 'state.moving = true' before calling 'turtle.forward()' and clearing it after, your startup recovery script can inspect neighboring blocks with 'turtle.inspect()' on reboot to deduce whether the interrupted move actually succeeded.
How it works
Understanding the 'Too long without yielding' error requires looking at how CC: Tweaked shares processing power with Minecraft. All Computers and Turtles in the world typically share a single background thread using cooperative multitasking via Lua coroutines. Only one Computer actually runs code at any exact microsecond. When your script calls functions like 'os.sleep()', 'rednet.receive()', or 'turtle.forward()', it internally calls 'coroutine.yield()', handing control back to the mod so the next Computer in line can run its code. If your script enters a loop that never calls a yielding function, it hogs the entire thread; after a few seconds, CC: Tweaked's built-in watchdog forcibly kills your program to prevent all other computers on the server from freezing.
Turtle resets and memory loss stem from how Minecraft handles block entities during chunk unloading. A Turtle's running Lua environment exists purely in volatile server RAM. When every player walks away from the area (and no chunk loader is present) or the server shuts down, the chunk is serialized to the world's hard drive and unloaded from memory. While CC: Tweaked saves the Turtle's physical virtual hard drive (the files you see when you type 'ls'), it cannot serialize the live, mid-execution Lua memory stack. Therefore, when the chunk loads back in, the Turtle undergoes a cold boot, clearing all temporary variables and running 'startup.lua' from scratch.
Common mistakes
- Using 'while true do' loops to constantly poll Redstone inputs without a sleep statement; instead of checking 'rs.getInput()' millions of times a second, wait for 'os.pullEvent("redstone")', which yields the computer safely until a redstone change actually happens.
- Spelling peripheral sides or network names with the wrong capitalization (such as 'Top' instead of 'top'), which causes 'peripheral.wrap()' to silently return 'nil' and crash your script several lines later.
- Deploying an unnamed Mining Turtle with a diamond pickaxe and thousands of coal fuel, then breaking it to move it one block over, permanently destroying all its code and stored fuel.
- Assuming 'turtle.forward()' always succeeds when tracking coordinates in a state file; if a falling gravel block or wandering mob blocks the Turtle, the move fails, so always check 'if turtle.forward() then' before updating your saved X/Y/Z numbers.
FAQ
Why does Ctrl+T not stop my stuck program?
Ctrl+T queues a 'terminate' event, which is only processed when your script yields (such as during a sleep or event pull). If your script is locked in a non-yielding loop, wait a few seconds for the watchdog to kill it, or hold Ctrl+S to force the entire Computer to shut down.
My script says 'attempt to call nil' instead of 'attempt to index nil'. What is the difference?
'Attempt to index nil' means the table or object before the dot or colon is missing (e.g., 'myMonitor' is nil in 'myMonitor.write()'). 'Attempt to call nil' means the table exists, but the function name you typed after the dot is misspelled or invalid (e.g., typing 'term.wirte()' instead of 'term.write()').
Can I just use a chunk loader instead of writing state persistence files for my Turtles?
A chunk loader prevents unloads while you explore the world, which solves most mid-game resets, but it will not protect your Turtle from server restarts or closing a singleplayer world. For total reliability, state files or GPS checks are still required.
How do I read the full error message if it scrolls off the top of the Computer screen?
Run your program inside a protected call using 'pcall(myFunction)' to catch the error string and write it to a log file, or attach an Advanced Monitor and redirect output to a larger screen scale using 'monitor.setTextScale(0.5)'.
By properly yielding your loops, validating wrapped peripherals, utilizing rescue floppy disks, and serializing Turtle states to disk, you can eliminate unexpected crashes and build truly autonomous CC: Tweaked systems.