Thread Tools Display Modes
02-15-24, 01:16 AM   #1
DennytXVII
A Murloc Raider
Join Date: Feb 2024
Posts: 6
Exclamation New to Lua, and making AddOns. Need Help.

I'm trying to create a simple Death Dice (More so known as Death Rolling) addon, but loading up WoW Classic and testing it with my brother I get the /help error stating it's an invalid/incorrect command. I do see the AddOn in the AddOns tab, I made sure it's the correct "interface" number, and in the .toc file the .lua name matches the .lua file name exactly. Any help would be amazing, please and thank you ♥ Here is the code:

StaticPopupDialogs["DEATHDICE_CONFIRM"] = {
text = "",
button1 = "YES",
button2 = "NO",
OnAccept = function(self, data)
StartGame(data.player, data.target, data.amount, data.unit)
SendMessage("You start a Death Dice game with " .. data.target .. "! Type '/roll' to make your first roll.")
end,
OnCancel = function(self)
SendMessage("You declined the Death Dice game.")
end,
timeout = 0,
whileDead = true,
hideOnEscape = true,
preferredIndex = 3,
}

-- Update the SlashCmdList.DEATHDICE function to handle initiating and declining a match
function SlashCmdList.DEATHDICE(msg)
if isGameInProgress then
SendMessage("A Death Dice game is already in progress!")
return
end

local player = UnitName("player")
local targetName = UnitName("target")
local amount, unit = tonumber(msg:match("(%d+)")), msg:match("(%a+)")

if not targetName then
SendMessage("You must target a friendly player to start a Death Dice game.")
return
end

if not amount or not unit or (unit ~= "copper" and unit ~= "silver" and unit ~= "gold") then
SendMessage("Usage: /deathdice <amount> <unit> (e.g., /deathdice 100 gold)")
return
end

local confirmation = StaticPopup_Show("DEATHDICE_CONFIRM", "Do you want to start a Death Dice game with " .. targetName .. " for " .. amount .. " " .. unit .. "?")
if confirmation then
confirmation.data = { player = player, target = targetName, amount = amount, unit = unit }
end
end

-- Update the StartGame function to accept the initiating player's name and the target's name
local function StartGame(player, target, amount, unit)
SendMessage("Death Dice game started! " .. player .. " vs " .. target .. ".")
SendMessage("Bet amount: " .. amount .. " " .. unit .. ".")
isGameInProgress = true
currentPlayer = player
betAmount = ConvertToCopper(amount, unit)
end


Again, to reiterate the title, this is my first addon, and honestly my first lua code. So if I did anything incorrect please help the best you can, I'm still not fully savvy with the lingo if you will lol.

I did try a lua compiler on some site, but I get a nil error about the StaticPopupDialog not being implemented or something along those lines. I know that shouldn't be an issue within WoW since it's built into their 'system'.
  Reply With Quote
02-15-24, 11:48 AM   #2
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,879
If you haven't already, to help with debugging in-game, install both
BugGrabber and BugSack

As to the code, SendMessage isn't a lua or WoW API function, except as a method of the C_Club system. Maybe it's a custom function in another addon?

To send text directly to the chatframe you can replace the SendMessage(...) lines with print(...)
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 02-15-24 at 12:30 PM.
  Reply With Quote
02-15-24, 05:00 PM   #3
DennytXVII
A Murloc Raider
Join Date: Feb 2024
Posts: 6
Originally Posted by Fizzlemizz View Post
If you haven't already, to help with debugging in-game, install both
BugGrabber and BugSack

As to the code, SendMessage isn't a lua or WoW API function, except as a method of the C_Club system. Maybe it's a custom function in another addon?

To send text directly to the chatframe you can replace the SendMessage(...) lines with print(...)
Thank you, I'll try that out. Can't hop on right this moment, but I can update the code. I also realized I was missing SLASH_DEATHDICE1 = "/deathdice" at the end of it. Makes sense why the text wasn't popping up lol.

When I tested this earlier btw, the window for yes and no buttons popped up, but they weren't clickable?

Last edited by DennytXVII : 02-15-24 at 05:06 PM.
  Reply With Quote
02-15-24, 05:17 PM   #4
Kanegasi
A Molten Giant
 
Kanegasi's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2007
Posts: 666
You did not set an actual command to use. The slash system needs a string that is your command, and when used, then calls the matching SlashCmdList entry.

Lua Code:
  1. SLASH_NameOfEntry1="/command"
  2. SLASH_NameOfEntry2="/cmd"
  3. SLASH_NameOfEntry3="/alias"
  4. SlashCmdList["NameOfEntry"]=function(textAfterCmd) --[[stuff]] end

You can make any number of different commands that call the same function, as long as the text between SLASH_ and the number is exactly the same as the entry in SlashCmdList, case sensitive. You only need just the number 1 line if you only want one command.
  Reply With Quote
02-15-24, 05:20 PM   #5
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,879
They work but the Yes will error on click because StartGame is declared as a local function and it appears in your code below the StaticPopupDialogs["DEATHDICE_CONFIRM"] declarations meaning it's not in scope (can't be seen).

Either move the function above the popup declaration (probably best) or remove the local declaration from StartGame making it global (in which case, StartGame is probably not a good name for it as all addons (inclucing the whole default game UI) share the same global space and Blizz. tends to use "common" names for their stuffs) so DeathDiceStartGame maybe?
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 02-15-24 at 05:23 PM.
  Reply With Quote
02-15-24, 06:07 PM   #6
DennytXVII
A Murloc Raider
Join Date: Feb 2024
Posts: 6
Originally Posted by Kanegasi View Post
You did not set an actual command to use. The slash system needs a string that is your command, and when used, then calls the matching SlashCmdList entry.

Lua Code:
  1. SLASH_NameOfEntry1="/command"
  2. SLASH_NameOfEntry2="/cmd"
  3. SLASH_NameOfEntry3="/alias"
  4. SlashCmdList["NameOfEntry"]=function(textAfterCmd) --[[stuff]] end

You can make any number of different commands that call the same function, as long as the text between SLASH_ and the number is exactly the same as the entry in SlashCmdList, case sensitive. You only need just the number 1 line if you only want one command.
Thanks for the point out, I did also point it out in my comment above this! I added it in
  Reply With Quote
02-15-24, 06:10 PM   #7
DennytXVII
A Murloc Raider
Join Date: Feb 2024
Posts: 6
Originally Posted by Fizzlemizz View Post
They work but the Yes will error on click because StartGame is declared as a local function and it appears in your code below the StaticPopupDialogs["DEATHDICE_CONFIRM"] declarations meaning it's not in scope (can't be seen).

Either move the function above the popup declaration (probably best) or remove the local declaration from StartGame making it global (in which case, StartGame is probably not a good name for it as all addons (inclucing the whole default game UI) share the same global space and Blizz. tends to use "common" names for their stuffs) so DeathDiceStartGame maybe?
I think I fixed this, but I'm not 100% just yet until I test it, here is the revised code I have currently, sorry for length:

-- Define the confirmation popup dialog
StaticPopupDialogs["DEATHDICE_CONFIRM"] = {
text = "Do you want to start a Death Dice game?",
button1 = "Yes",
button2 = "No",
OnAccept = function(self, data)
StartGame(data.player, data.target, data.amount, data.unit)
print("You start a Death Dice game with " .. data.target .. "! Type '/roll' to make your first roll.")
StaticPopup_Hide("DEATHDICE_CONFIRM") -- Close the confirmation popup
end,
OnCancel = function(self)
print("You declined the Death Dice game.")
StaticPopup_Hide("DEATHDICE_CONFIRM") -- Close the confirmation popup
end,
timeout = 0,
whileDead = true,
hideOnEscape = true,
preferredIndex = 3,
}

-- Define the SlashCmdList.DEATHDICE function to handle initiating and declining a match
function SlashCmdList.DEATHDICE(msg)
if isGameInProgress then
print("A Death Dice game is already in progress!")
return
end

local player = UnitName("player")
local targetName = UnitName("target")
local amount, unit = tonumber(msg:match("(%d+)")), msg:match("(%a+)")

if not targetName then
print("You must target a friendly player to start a Death Dice game.")
return
end

if not amount or not unit or (unit ~= "copper" and unit ~= "silver" and unit ~= "gold") then
print("Usage: /deathdice <amount> <unit> (e.g., /deathdice 5 copper)")
return
end

local confirmation = StaticPopup_Show("DEATHDICE_CONFIRM", "Do you want to start a Death Dice game with " .. targetName .. " for " .. amount .. " " .. unit .. "?")
if confirmation then
confirmation.data = { player = player, target = targetName, amount = amount, unit = unit }
end
end
SLASH_DEATHDICE1 = "/deathdice"
  Reply With Quote
02-15-24, 07:13 PM   #8
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,879
I can't see how. StartGame is completely missing from the code posted .

The ConvertToCopper function is also missing (and not an API function) so that will cause an error.

In future, place your code inside code tags (either the # or Lua buttons above the edit box).

Lua Code:
  1. -- Update the StartGame function to accept the initiating player's name and the target's name
  2. local function StartGame(player, target, amount, unit)
  3.     print("Death Dice game started! " .. player .. " vs " .. target .. ".")
  4.     print("Bet amount: " .. amount .. " " .. unit .. ".")
  5.     isGameInProgress = true
  6.     currentPlayer = player
  7.     betAmount = ConvertToCopper(amount, unit)
  8. end
  9.  
  10. StaticPopupDialogs["DEATHDICE_CONFIRM"] = {
  11.     text = "Do you want to start a Death Dice game?",
  12.     button1 = "Yes",
  13.     button2 = "No",
  14.     OnAccept = function(self, data)
  15.         StartGame(data.player, data.target, data.amount, data.unit)
  16.         print("You start a Death Dice game with " .. data.target .. "! Type '/roll' to make your first roll.")
  17.         StaticPopup_Hide("DEATHDICE_CONFIRM") -- Close the confirmation popup
  18.     end,
  19.     OnCancel = function(self)
  20.         print("You declined the Death Dice game.")
  21.         StaticPopup_Hide("DEATHDICE_CONFIRM") -- Close the confirmation popup
  22.     end,
  23.     timeout = 0,
  24.     whileDead = true,
  25.     hideOnEscape = true,
  26.     preferredIndex = 3,
  27. }
  28.  
  29. -- Define the SlashCmdList.DEATHDICE function to handle initiating and declining a match
  30. function SlashCmdList.DEATHDICE(msg)
  31.     if isGameInProgress then
  32.         print("A Death Dice game is already in progress!")
  33.         return
  34.     end
  35.  
  36.     local player = UnitName("player")
  37.     local targetName = UnitName("target")
  38.     local amount, unit = tonumber(msg:match("(%d+)")), msg:match("(%a+)")
  39.  
  40.     if not targetName then
  41.         print("You must target a friendly player to start a Death Dice game.")
  42.         return
  43.     end
  44.  
  45.     if not amount or not unit or (unit ~= "copper" and unit ~= "silver" and unit ~= "gold") then
  46.         print("Usage: /deathdice <amount> <unit> (e.g., /deathdice 5 copper)")
  47.         return
  48.     end
  49.  
  50.     local confirmation = StaticPopup_Show("DEATHDICE_CONFIRM", "Do you want to start a Death Dice game with " .. targetName .. " for " .. amount .. " " .. unit .. "?")
  51.     if confirmation then
  52.         confirmation.data = { player = player, target = targetName, amount = amount, unit = unit }
  53.     end
  54. end
  55. SLASH_DEATHDICE1 = "/deathdice"
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 02-15-24 at 08:17 PM.
  Reply With Quote
02-15-24, 11:47 PM   #9
DennytXVII
A Murloc Raider
Join Date: Feb 2024
Posts: 6
Noted! I will definitely keep that in mind Thank you for the help, I truly appreciate it lol. I'm in fact a noobie lol. I did away with the ConvertToCopper, added the StartGame that I SOMEHOW deleted, it's mostly working (the error comments at least lol) but I go to do /deathdice 5 copper and it won't even let me click enter, just stays in the chat bar while targeting my friend to test. Though, when not targeting my friend it enters, but shows the "You must target a friendly player to start a Death Dice game." error message. I'm missing stuff, or maybe something is in the wrong place is all I can think , also I downloaded the BugGrabber and BugSack, since I forgot to earlier. Will definitely install them and test with them! Here's what I have updated lol

Lua Code:
  1. -- Global variable to keep track of whose turn it is
  2. local currentPlayer = ""
  3. local isGameInProgress = false
  4.  
  5. -- Function to start the game
  6.  function StartGame(player, target, amount, unit)
  7.     currentPlayer = player
  8.     isGameInProgress = true
  9.    
  10.     print(player .. " starts a Death Dice game with " .. target .. "! Type '/ddroll' to make your first roll.")
  11.     print("Bet amount: " .. amount .. " " .. unit .. ".")
  12. end
  13.  
  14. -- Function to handle rolling the dice
  15. local function RollDice(player)
  16.     if player ~= currentPlayer then
  17.         print("It's not your turn to roll!")
  18.         return
  19.     end
  20.    
  21.     local roll = math.random(1, 100)
  22.     print(player .. " rolls: " .. roll)
  23. end
  24.  
  25. -- Define the confirmation popup dialog
  26. StaticPopupDialogs["DEATHDICE_CONFIRM"] = {
  27.     text = "",
  28.     button1 = "Yes",
  29.     button2 = "No",
  30.     OnAccept = function(self, data)
  31.         StartGame(data.player, UnitName("player"), data.amount, data.unit)
  32.         StaticPopup_Hide("DEATHDICE_CONFIRM")  -- Close the confirmation popup
  33.     end,
  34.     OnCancel = function(self)
  35.         print("You declined the Death Dice game.")
  36.         StaticPopup_Hide("DEATHDICE_CONFIRM")  -- Close the confirmation popup
  37.     end,
  38.     timeout = 0,
  39.     whileDead = true,
  40.     hideOnEscape = true,
  41.     preferredIndex = 3,
  42. }
  43.     end
  44. -- Define the SlashCmdList.DEATHDICE function to handle initiating and declining a match
  45. function SlashCmdList.DEATHDICE(msg)
  46.     if isGameInProgress then
  47.         print("A Death Dice game is already in progress!")
  48.         return
  49.     end
  50.  
  51.     local player = UnitName("player")
  52.     local targetName = UnitName("target")
  53.     local amount, unit = tonumber(msg:match("(%d+)")), msg:match("(%a+)")
  54.  
  55.     if not targetName then
  56.         print("You must target a friendly player to start a Death Dice game.")
  57.         return
  58.     end
  59.  
  60.     if not amount or not unit or (unit ~= "copper" and unit ~= "silver" and unit ~= "gold") then
  61.         print("Usage: /deathdice <amount> <unit> (e.g., /deathdice 5 copper)")
  62.         return
  63.     end
  64.  
  65.     -- Send a request to the target player
  66.     SendRequestToTarget(player, targetName, amount, unit)
  67. end
  68.  
  69. -- Function to send a request to the target player
  70. local function SendRequestToTarget(player, targetName, amount, unit)
  71.     -- Display a confirmation popup on the target player's screen
  72.     local confirmation = StaticPopup_Show("DEATHDICE_CONFIRM", "Do you want to start a Death Dice game with " .. player .. "?")
  73.     if confirmation then
  74.         confirmation.data = { player = player, target = targetName, amount = amount, unit = unit }
  75.     end
  76. end
  77.  
  78. -- Define the /ddroll command to allow players to roll the dice during a Death Dice game
  79. function SlashCmdList.DDROLL(msg)
  80.     if not isGameInProgress then
  81.         print("No Death Dice game in progress!")
  82.         return
  83.     end
  84.    
  85.     RollDice(UnitName("player"))
  86. end
  87.  
  88. -- Define the slash commands
  89. SLASH_DEATHDICE1 = "/deathdice"
  90. SLASH_DDROLL1 = "/ddroll"

Last edited by DennytXVII : 02-15-24 at 11:54 PM.
  Reply With Quote
02-16-24, 12:03 AM   #10
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,879
If you get and error, execution of the current process stops there. If you click Enter and get an error in the running code then the StaticPopupDialogs code won't get to the part where it closes the popup frame.

Installing the debugging addons will let you know when you've hit an error.
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 02-16-24 at 12:06 AM.
  Reply With Quote
02-18-24, 01:05 AM   #11
Paviawex
A Kobold Labourer
Join Date: Feb 2024
Posts: 1
Have you considered checking if there are any errors in your code that might be preventing the execution of certain functions, especially when clicking Enter in the chat, and have you installed the debugging addons for better visibility?
  Reply With Quote
02-29-24, 10:59 AM   #12
DennytXVII
A Murloc Raider
Join Date: Feb 2024
Posts: 6
Originally Posted by Paviawex View Post
Have you considered checking if there are any errors in your code that might be preventing the execution of certain functions, especially when clicking Enter in the chat, and have you installed the debugging addons for better visibility?
I have, but I don't fully know what to look for. I'm very new to all this, and I haven't been able to test it as of right now, since it's my brother's account lol. I'll be testing it out the next time him and I play though!
  Reply With Quote
02-29-24, 11:13 AM   #13
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,879
If you've installed the debugging addons and get an error the BugSack icon will turn from green to red (that and you should hear a warning sound...get used to that ).

Clicking the BugSack icon will open the error frame with the last? error text displayed. You can select/copy/paste the error text here along with your code and maybe we can help you figure out why you got the error.

If you have several errors BugSack has Previous/Next buttons for moving through them and a button to limit the view to just the current errors.
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.
  Reply With Quote

WoWInterface » AddOns, Compilations, Macros » AddOn Help/Support » New to Lua, and making AddOns. Need Help.


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off