Bad argument to random?

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
  • The dictionary returns a nil value
Code

Module

return {
	mushroom = {
		["name"] = "Mushroom",
		["image"] = "rbxassetid://4082219038",
		["value"] = 25,
		["model"] = models:WaitForChild("mushroom")
	},

	dirtblob = {
	["name"] = "Blob of Dirt",
	["image"] = "rbxassetid://4089379109",
	["value"] = 25,
	["model"] = models:WaitForChild("dirtBlob")
	},
	
}

Dialogue Function Code

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

Thank you for reading :slight_smile:

1 Like

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

Did you use itemdata = require(Module)? :thinking:

If you do have it somewhere in the code, please include it.

Yea. It was all the way at the beginning of the script

Try using print(#itemdata) and check. Otherwise, it’s hierarchical issues where the module is not really in ReplicatedStorage.

Done that, here’s what I got.
33%20PM

The # operator only works for arrays, not dictionaries.

1 Like

You are trying to get the length of a dictionary, instead of an array.

Because ItemData is this:

return {
	mushroom = {
		["name"] = "Mushroom",
		["image"] = "rbxassetid://4082219038",
		["value"] = 25,
		["model"] = models:WaitForChild("mushroom")
	};

	dirtblob = {
	    ["name"] = "Blob of Dirt",
	    ["image"] = "rbxassetid://4089379109",
	    ["value"] = 25,
	    ["model"] = models:WaitForChild("dirtBlob")
	};
}

And not an array, like this:

return {
	{
		["name"] = "Mushroom",
		["image"] = "rbxassetid://4082219038",
		["value"] = 25,
		["model"] = models:WaitForChild("mushroom")
	},
	{
	    ["name"] = "Blob of Dirt",
	    ["image"] = "rbxassetid://4089379109",
	    ["value"] = 25,
	    ["model"] = models:WaitForChild("dirtBlob")
	}
}

:arrow_up: should work.

2 Likes

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.

1 Like

Thanks it finally works!(Though there’s a slight error in the server module, it did not really affect the game too much)

Will keep this in mind the next time I use dictionaries.

1 Like