Module:RPG: Difference between revisions

From Empire of Dragons
Jump to navigation Jump to search
(Created page with "-- Module for rolling RPG dice local p = {} -- Function to roll dice function p.rollDice(diceString) local num, sides, modifier = string.match(diceString, "(%d+)d(%d+)([%+%-]?%d*)") num = tonumber(num) or 1 sides = tonumber(sides) or 6 modifier = tonumber(modifier) or 0 local total = modifier for _ = 1, num do total = total + math.random(sides) end return total end return p")
 
No edit summary
 
(11 intermediate revisions by the same user not shown)
Line 3: Line 3:


-- Function to roll dice
-- Function to roll dice
function p.rollDice(diceString)
function p.rollDice(frame)
    local args = frame.args
    local diceString = args[1] or ""
     local num, sides, modifier = string.match(diceString, "(%d+)d(%d+)([%+%-]?%d*)")
     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
     num = tonumber(num) or 1
     sides = tonumber(sides) or 6
     sides = tonumber(sides) or 6
     modifier = tonumber(modifier) or 0
     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 total = modifier
    local individualResults = {}


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


     return total
    resultString = resultString .. ": " .. table.concat(individualResults, ", ") .. " = " .. total
 
     return resultString
end
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
return p

Latest revision as of 08:19, 29 January 2024

Rolling 3d6+3: 5, 5, 5 = 18

  • Rolling 3d6: 5, 5, 5 = 15


  • Rolling 2d6+2: 5, 5 = 12
  • Rolling 4d6+2: 5, 5, 5, 2 = 19
  • Rolling 2d4+4: 4, 4 = 12
  • (3d6--2): 5, 5, 5 = 13
  • (3d6--3): 5, 5, 5 = 12
  • (3d6+1): 5, 5, 5 = 16
  • (3d6): 5, 5, 5 = 15

-- 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