• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Solved Talkaction dice roller scripting help

deadsmelly

New Member
Joined
Apr 5, 2009
Messages
8
Reaction score
4
Hello all!

I am attempting to run a D&D campaign using TFS 1.1 and have a talk action that can roll dice. I've managed to code together a simple dice roller that broadcasts the results of rolling a d20.

Code:
function onSay(player, words, param)
   local value = math.random(1, 20)
    
   do
     broadcastMessage(player:getName() .. " rolled a d20. It lands on " .. value .. ".", MESSAGE_STATUS_CONSOLE_BLUE )
   end
end

What I'd really like to do is have it so players could type /roll and then the type of dice they want and it would give the result back. Example: /roll 2d20 to roll two 2o sided dice. I am not sure how to get the script to listen for different strings and react accordingly though.

Any help would be appreciated!

SOLVED : Used Xikini's excellent script below.
 
Last edited:
Solution
Try this. (It's untested)
Code:
local roll = {
     ["1d20"] = {dice_amount = 1, dice_value = 20},
     ["2d20"] = {dice_amount = 2, dice_value = 20},
     ["1d6"] = {dice_amount = 1, dice_value = 6}
}

function onSay(player, words, param)
     param = param:lower()
 
     if param == "" or not param then
         player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "Blank is not a valid dice roll.")
         return true
     end
 
     if roll[param] then
         local text = ""
         for var = 1, roll[param].dice_amount do
             local rand = math.random(roll[param].dice_value)
             if text ~= "" then
                 text = text .. " and "
             end
             text = text .. "" .. rand .. ""
         end...
Try this. (It's untested)
Code:
local roll = {
     ["1d20"] = {dice_amount = 1, dice_value = 20},
     ["2d20"] = {dice_amount = 2, dice_value = 20},
     ["1d6"] = {dice_amount = 1, dice_value = 6}
}

function onSay(player, words, param)
     param = param:lower()
 
     if param == "" or not param then
         player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "Blank is not a valid dice roll.")
         return true
     end
 
     if roll[param] then
         local text = ""
         for var = 1, roll[param].dice_amount do
             local rand = math.random(roll[param].dice_value)
             if text ~= "" then
                 text = text .. " and "
             end
             text = text .. "" .. rand .. ""
         end
         broadcastMessage(player:getName() .. " rolled a " .. param .. ". They have rolled a " .. text .. ".", MESSAGE_STATUS_CONSOLE_BLUE )
     else
         param = param:gsub("^%l", string.upper)
         player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "" .. param .. " is not a valid dice roll.")
     end
 
     return true
end
 
Last edited:
Solution
Getting an error in the server console while it is loading.
[Warning - Event : : checkScript] Can not load script: scripts/roll_dice.lua data/talkactions/scripts/roll_dice.lua:17: '<name>' expected near '1'.
 
Getting an error in the server console while it is loading.
[Warning - Event : : checkScript] Can not load script: scripts/roll_dice.lua data/talkactions/scripts/roll_dice.lua:17: '<name>' expected near '1'.
That means the table value is defined incorrectly.
Did you test my script before altering it?

If you altered it, post the table your using.
 
No alterations were made, I just copied it into notepad ++, saved as roll_dice.lua and edited the talkactions.xml with <talkaction words="/roll" script="roll_dice.lua" /> Did I make a mistake somewhere?
 
No alterations were made, I just copied it into notepad ++, saved as roll_dice.lua and edited the talkactions.xml with <talkaction words="/roll" script="roll_dice.lua" /> Did I make a mistake somewhere?
Was my derp. :(

change
Code:
for 1, roll[param].dice_amount do
to
Code:
for var = 1, roll[param].dice_amount do

Or copy paste above script.
I've edited it with the alteration.
 
Back
Top