Module:RPG

From Empire of Dragons
Revision as of 15:54, 27 January 2024 by Oberoten (talk | contribs)
Jump to navigation Jump to search

Rolling 3d6+3: 6, 4, 6 = 19

  • Rolling 3d6: 6, 4, 6 = 16


  • Rolling 2d6+2: 6, 4 = 12
  • Rolling 4d6+2: 6, 4, 6, 4 = 22
  • Rolling 2d4+4: 4, 3 = 11
  • Script error: The function "rollStats" does not exist.
  • Script error: The function "rollStats" does not exist.
  • Script error: The function "rollStats" does not exist.
  • Script error: The function "rollStats" does not exist.

-- Module for rolling RPG dice
local p = {}

-- Function to roll dice
function p.rollDice(frame)
    local args = frame.args
    local diceString = args[1] or ""
    local num, sides, modifier = string.match(diceString, "(%d+)d(%d+)([%+%-]?%d*)")
    -- Seed the random number generator with the current time
    math.randomseed(os.time())
    num = tonumber(num) or 1
    sides = tonumber(sides) or 6
    modifier = tonumber(modifier) or 0

    local resultString = "Rolling " .. num .. "d" .. sides
    if modifier ~= 0 then
    	if modifier>0 then
          resultString = resultString .. "+" .. modifier
        else
          resultString = resultString .. "-" .. modifier
        end
    end

    local total = modifier
    local individualResults = {}

    for i = 1, num do
        local roll = math.random(1, sides)
        total = total + roll
        table.insert(individualResults, roll)
    end

    resultString = resultString .. ": " .. table.concat(individualResults, ", ") .. " = " .. total

    return resultString
end

return p