I wrote a quest-giving function for my quest dialogue system and ran into this error while testing the dialogue. Error [14:08:58.742 - ReplicatedStorage.RobloxDialogue.DynamicTextFunctions:41: bad argument #2 to 'random' (interval is empty)]
This error occurs whenever I press the option that is linked to the function. The dialogue also will not change. Though, I tried to guess what caused the error
The dictionary is not written in an array
The dictionary ‘name’ in the module is not specified
local itemdata = require(rps.itemDatabase)
if player:FindFirstChild("toad_quest") then
return "You already have a quest! Return to me once you have completed it."
elseif not player:FindFirstChild("toad_quest") then
local chosen
for i, v in pairs(itemdata) do
chosen = itemdata[math.random(1, #itemdata)]--error line
print(chosen["name"])
end
--after item is chosen
local reward = math.random(500,10000)
gui.rewardlabel.Text = tostring(reward)
gui.item1.Image = chosen["image"]
rps.toad_quest:Clone().Parent = player
player.toad_quest.item1.Name = chosen["name"]
player.toad_quest.reward.Value = reward
end
end
Side Note: I have already required the module at the beginning of the script. This is a snippet of where the error came from
The problem is that #itemdata might be 0, and therefore you can to diagnose why itemdata is empty, perhaps it’s nil. What exactly is itemdata assigned with?
itemdata is the variable name I used for the module script for the function. It is a dictionary but I can’t seem to pick out why there was an error. I suspected it was nil but I don’t have an idea why it is
Unfortunately, when I was testing the # operator on dictionary, this is true. Looks like I didn’t know this behavior.
-- Tested code
local dict = {
q = 1;
b = 294;
seven = 92;
}
print(#dict) -- 0
@KawaiiX_Jimin I believe you should use an array, if a dictionary is not necessary(key is never used). However, if you insist on using a dictionary. I believe you should try using a function converting the dictionary into an array.