Module:RPG: Difference between revisions

From Empire of Dragons
Jump to navigation Jump to search
No edit summary
No edit summary
Line 3: Line 3:


-- Function to roll dice
-- Function to roll dice
function p.rollDice(diceString)
function p.rollDice(args)
     if type(diceString) == "string" then
     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*)")
        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 total = modifier
    local total = modifier


        for _ = 1, num do
    for _ = 1, num do
            total = total + math.random(1, sides)
        total = total + math.random(1, sides)
        end
    end


        return total
    return total
    else
        return "Invalid input. Please provide a valid dice string."
    end
end
end


return p
return p

Revision as of 15:43, 27 January 2024

1

  • 1


  • 1
  • 1
  • 1
  • 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(args)
    local diceString = args[1] or ""
    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(1, sides)
    end

    return total
end

return p