Module:RPG

From Empire of Dragons
Revision as of 08:18, 29 January 2024 by Oberoten (talk | contribs)
Jump to navigation Jump to search

Lua error at line 11: bad argument #1 to 'randomseed' (number expected, got no value).

  • Lua error at line 11: bad argument #1 to 'randomseed' (number expected, got no value).


  • Lua error at line 11: bad argument #1 to 'randomseed' (number expected, got no value).
  • Lua error at line 11: bad argument #1 to 'randomseed' (number expected, got no value).
  • Lua error at line 11: bad argument #1 to 'randomseed' (number expected, got no value).
  • (3d6--2): 5, 3, 3 = 9
  • (3d6--3): 5, 3, 3 = 8
  • (3d6+1): 5, 3, 3 = 12
  • (3d6): 5, 3, 3 = 11

-- 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(tonumber(mw.getCurrentFrame():preprocess('{{#time:YmdHis}}')))
    math.randomseed()
    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

function p.rollStats(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(tonumber(mw.getCurrentFrame():preprocess('{{#time:YmdHis}}')))
    num = tonumber(num) or 1
    sides = tonumber(sides) or 6
    modifier = tonumber(modifier) or 0

    local resultString = "(" .. num .. "d" .. sides
    if modifier ~= 0 then
    	if modifier>0 then
          resultString = resultString .. "+" .. modifier
        else
          resultString = resultString .. "-" .. modifier
        end
    end
    resultString = resultString .. ")"
    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