Trouble with getting random thing from table

I’m trying to choose a random rarity from in a table, and then choose a random thing from in said table however idk how to do it but I do have some code that is not working, im sure its simply misunderstanding of syntax

(i just started making the game yesterday so ill make an actual random generator with chances of getting things dont make fun of it)

this is in a module script all the code i am showing

local rarity = math.random(1, 7) --[[ just a placeholder for now as i said for the future randomization 
system ]]
print(Fish.Rarities[6]) --[[
i added this line just to test why it wasnt working, it printed attempt to index nil 
when i did fish.rarities[rarity] and with this value too
]]
local fish = Fish.Rarities[rarity][math.random(1, #Fish.Rarities[rarity])] --[[
on this line it says attempt to index nil with number
]]

here is the table, all things are in quotation marks. the Rarities index as well as the rarities indexes themselves used to be outside of quotation marks (and therefore also simply 1 equals sign instead of 2) before this, i did some testing while making this post and this is the final code as i have written it

local Fish = {
	"Rarities" == {
		"Common" == {
			"Carp",
			"Catfish",
			"Cuttlefish",
			"Cod",
			"Squid",
			"Salmon",
			"Herring",
			"Shrimp",
			"Bass"
		},
		"Uncommon" == {
			"Frog",
			"Starfish",
			"Clownfish",
			"Butterfly fish",
			"Trout",
			"Goldfish",
			"Oyster",
			"Mackerel"
		},
		"Rare" == {
			"Lionfish",
			"Stargazer fish",
			"Flying fish",
			"Blobfish",
			"Seahorse",
			"Angler fish",
			"Sea urchin"
		},
		"Epic" == {
			"Nurse shark",
			"Lemon shark",
			"Sunfish"
		},
		"Legendary" == {
			"Sea cow",
			"Orca",
			"Tiger shark",
			"Bull shark",
			"Alligator",
			"Crocodile"
		},
		"Mythic" == {
			"Anglerfish",
			"Whale shark",
			"Blue whale",
			"Hammerhead shark"
		},
		"Unique" == {
			"Kraken",
			"Megalodon",
			"Polar bear"
		}
	},
}
1 Like

One thing is because you’re using double equals. Double equals are used to evaluate a statement (check something). They return a Boolean. It should be something like:

local fish = {
    ["Rarities"] = {
        ["Common"] = {
            --etc.
        }
    --etc.
    }
}

You mentioned this in your post. Can you explain why you used double equals and what the error actually is?




the main reason:
You are misunderstanding how dictionaries can be referenced. When you make an array:

local array = {
    "hi",
    "bye",
    "good day"
}

Lua actually puts it into this format:

local array = {
    [1] = "hi",
    [2] = "bye",
    [3] = "good day"
}

which is what allows you to do something like print(array[2]). You are referencing the key to get its associated value.

Well, we do the same in dictionaries.
So, to get all the rarities, we could do:

print(Fish.Rarities.Common)
--or
print(Fish.Rarities["Common"])
--these output the same thing.

This means you cannot reference a number as it will error - there is no value associated with the number, returning nil, and then you attempt to index nil with something, bosh, an error.

A fix for this would be making a table of rarities to refer to.

local rarities = {"Common", "Uncommon", "Rare" --[[etc.]])

local rarity = rarities[math.random(1, #rarities)]
local chosenRarity = Fish[rarity] --say "Common" was chosen
print(chosenRarity) --this would output the table of Common fish

i used double equals because i didnt know about the quotation marks inside of square brackets, or atleast it didnt occur to me at the time, and also because that is what showed up. when i put the " by themselves without the brackets, it underlined everything in the entire table in red

ive not implemented what youve shown to me in my code yet but i will do it right after this reply and if it doesnt work ill say what the error is. are you sure the last line in the first block of code is correct


after i actually cast the line out in this other picture when i click

it shows that error. it has nothing to do with any of the scripts inside of the fishing rod, which is irrelevant to anything in this post really. the error stems from the module script which i put the code that the error may be coming from in the post

I updated my previous post, please take a look :smiley: .

Here is a more, in-depth post about this that I made:
Arrays and Dictionaries - Help and Feedback / Scripting Support - Developer Forum | Roblox

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.