Dice-Rolling RPG Character Generator Script
Build a Lua script that creates random characters for tabletop role-playing games like Dungeons & Dragons: it rolls ability scores the classic way (four six-sided dice, keeping the best three), picks a class and background (some more likely than others, using weighted random choice), applies their bonuses, and prints a neat character sheet. You can fix a seed to get the same character again, and save characters to a file. It's a fun first Lua project: you'll learn functions, tables used as lists and dictionaries, random numbers with math.random, weighted choices, string formatting, and writing files.
- Estimate
- ~5.5h
- Steps
- 6
- Completed by
- 0
- Proposed by
- codeseed.app
Lua standard library
Project roadmap
- 01
Roll the ability scores
~1hWrite a function roll(n, sides) that rolls n dice with math.random, and a function that rolls 4d6 and drops the lowest die. Use it to generate the six abilities (strength, dexterity, constitution, intelligence, wisdom and charisma) and compute each ability's modifier ((score - 10) // 2). Seed the generator with a --seed option or os.time(). Done when: rolling prints six scores between 3 and 18 with correct modifiers.
- 02
Define classes and backgrounds
~1hCreate tables of classes (such as fighter, wizard, rogue and cleric) with a weight, a main ability, hit dice and starting equipment, and backgrounds (such as soldier, sage and criminal) with a weight, skill bonuses and a short description. Keep the data in a separate file loaded with dofile or require. Done when: the tables load and can be printed.
- 03
Choose with weights
~1hWrite a weighted random choice function (add up the weights, pick a random number in that range, and walk the list until the running total passes it). Use it to pick a class and background, and optionally assign the best rolled score to the class's main ability. Check the weights by picking 10,000 times and printing how often each was chosen. Done when: the choice frequencies match the weights closely.
- 04
Print the character sheet
~1hGenerate a random name from lists of first names and nicknames, and print a formatted character sheet with string.format: name, class, background, the six abilities with modifiers, hit points (the class hit die plus the constitution modifier), skills and equipment, in aligned columns with a simple border. Done when: the character sheet is neatly formatted.
- 05
Save to a file
~0.5hAdd --save FILE, which appends the character sheet to a text file with io.open in append mode, and --count N to create several characters at once. Report clearly when the file can't be written. Done when: characters are saved to the file, and several can be created in one run.
- 06
Test it
~1hWrite a test file with plain assert (or busted): dice results always stay in range, 4d6-drop-lowest never goes below 3 or above 18, modifiers for scores 3 to 18, the weighted choice never picks an item with weight 0, and a fixed seed always gives the same character. Done when: the tests pass.
Ready to build this?
Get a GitHub repo and start building. Your AI reviewer checks each step as you go.
Tech stack