Attempt to index nil (table) with 'naame' (part of table) (Line 128)

I was trying everything I could think of, and nothing work, could someone please tell me what’s wrong with this code? The error is: attempt to index nil (table) with ‘naame’ (part of table) (Line 128)


local replicatedStorage = game:GetService("ReplicatedStorage")
local DogeSkinsFolder = replicatedStorage:FindFirstChild("DogeSkins")
local Events = replicatedStorage:FindFirstChild("Events")

local Parent = Events:FindFirstChild("Parent")
local TakeAway = Events:FindFirstChild("TakeAway")

-- Normal = White
--Rare = Blue
--Super Rare = Red
--Legendary = Yellow
--Godly = Dark Purpel

local SkinDoges = {
	GalaxyDoge = {
		Img = "http://www.roblox.com/asset/?id=12529836139",
		Name = "Galaxy Doge",
		Rarity = "Godly",
		ColorCode = "Godly",
		naame = "GalaxyDoge"
	},
	RedDoge = {
		Img = "http://www.roblox.com/asset/?id=12529665792",
		Name = "Red Doge",
		Rarity = "Legendary",
		ColorCode = "Legendary",
		naame = "RedDoge"
	},
	SparkleTime = {
		Img = "http://www.roblox.com/asset/?id=12526506473",
		Name = "Sparke Time Doge",
		Rarity = "Super Rare",
		ColorCode = "Super Rare",
		naame = "SparkleTime"
	},

	Amethyst = {
		Img = "http://www.roblox.com/asset/?id=12432132041",
		Name = "Amethyst Doge",
		Rarity = "Rare",
		ColorCode = "Rare",
		naame = "Amethyst"
	},
	Frost = {
		Img = "http://www.roblox.com/asset/?id=11550564067",
		Name = "Frost Doge",
		Rarity = "Normal",
		ColorCode = "Normal",
		naame = "Frost"
	}
}

local RarityTable = {

	Normal = {
		SkinDoges["Frost"],},
	Rare = {
		SkinDoges["Amethyst"],},
	SuperRare = {SkinDoges["SparkleTime"],
		
	},
	Legendary = {
		SkinDoges["RedDoge"],},
	Godly = {
		SkinDoges["GalaxyDoge"],
	}

}

--[[	Normal = SkinDoges["Frost"],
	Rare = SkinDoges["Amethyst"],
	SuperRare = SkinDoges["SparkleTime"],
	Legendary = SkinDoges["RedDoge"],
	Godly = SkinDoges["GalaxyDoge"]]

local Rarities = {

	Normal = 50;
	Rare = 33;
	SuperRare = 11;
	Legendary = 5;
	Godly = 1

}

script.Parent.MouseButton1Click:Connect(function()
	local leaderstats = plr.leaderstats
	local DogeCoins = leaderstats.DogeCoins
	if DogeCoins then
		if DogeCoins.Value >= 10000 then
			
			local RandomNumber = math.random(1,100)
			
			local counter = 0
			
			local total = 0
			for i, v in pairs(Rarities) do
				print(v)
				total += v
			end

			local function getRandomItem()
				local r = math.random(1, total)
				local add = 0
				for item, v in pairs(Rarities) do
					if r > add and r <= add + v then
						return item
					end
					add += v
				end
			end
			

			local item = getRandomItem()
			warn(item)
			warn()
			local thereal = math.random(1,#RarityTable[item])
			warn(Rarities)
			warn(RarityTable)
			warn(item)
			warn(SkinDoges)
			warn(thereal)
			local thereallyreal = SkinDoges[thereal]


			Parent:FireServer(thereallyreal ,thereallyreal["naame"], plr.DogeSkins)

			--[[for rarity, weight in pairs(Rarities) do
				counter = counter + weight

				if RandomNumber <= counter then
					
					local RarityTable = RarityTable[rarity]
					local chosenPet = RarityTable[math.random(1, #RarityTable)]
					

					Parent:FireServer(chosenPet ,chosenPet["naame"], plr.DogeSkins)
				end
			end--]]
			

		else
			print("You cannot buy :(")
		end
	end
end)```

This won’t work:

local thereal = math.random(1,#RarityTable[item])

because your table isn’t numbers but names, liek GalaxyDoge.

As mentioned by @/DeEchteBelg, you cannot get a random value in a dictionary like that. See my post that addresses the same issue.
https://devforum.roblox.com/t/argument-2-invalid-error/2402205/5

Assuming that you could get the “length”, since your table is indexed with strings rather than number, your code would still fail because Anims[1] and Anims[2] would resolve to nil. Instead, you need to make some code that actually picks a random value in a table, regardless of what keys it has. The following code is how I would go about choosing a random key in a given table.

local function RandomKey(tbl)
   local keys = {}
   for key, _value in pairs(tbl) do
       table.insert(keys, key)
   end
   return keys[math.random(1, #keys)]
end

Then how do I fix it?

don’t mind this I need more letters yk

Then how do I fix it?

don’t mind this I need more letters yk

It should be as simple as adding that RandomKey function and replacing any instances of math.random(1, #sometable) with RandomKey(sometable), i.e.

-- Change this
local chosenPet = RarityTable[math.random(1, #RarityTable)]
-- To this
local chosenPet = RarityTable[RandomKey(RarityTable)]

Still dosen’t work, i’m getting this now: Players.FazpyDev.PlayerGui.DogecrateShop.Background.Shop.Crate1.Crate1:123: attempt to index nil with 'naame'

Sorry, I’ve taken another look at your code and now I actually see where the problem lies.
On this line from the original code

local thereal = math.random(1,#RarityTable[item])

You’re picking a random index from RarityTable[item], which is a number. Your intention seems to be to actually use the value, which seems to be why you made the thereallyreal variable. However, thereallyreal ends up being nil because SkinDoges is indexed by the names, not a number.

The solution to the issue you’re facing would be to change the line above to this,

local thereal = RarityTable[item][math.random(1, #RarityTable[item])]

since you store a reference to SkinDoges in the RarityTable anyways, and replace thereallyreal with thereal. Sorry I didn’t take a more in-depth look at the code before, hopefully it’s working as intended now.

Nope, still get the same error