Table returning nil depsite there being things in the table

So, I’m attempting to make a game, and to keep my code clean and easily editable I’m using tables, which I have used many times, and I haven’t had a problem like this before.

My problem is that printing the table returns all 3 values, but trying to get one of the values with math.random() returns nil.

local EndScenarios = {
	["Cancelled"] = {["Text1"] = 'T1'; ["Text2"] = 'T2'; ["Text3"] = 'T3'};
	["NFT"] = {["Text1"] = 'T1'; ["Text2"] = 'T2'; ["Text3"] = 'T3'};
	["HotTub"] = {["Text1"] = 'T1'; ["Text2"] = 'T2'; ["Text3"] = 'T3'};
}
function CallDad(Player)
	if Player.Data.Money.Value >= 10000000 then return true, EndScenarios[math.random(1, #EndScenarios)] end
	return false, nil
end

Printing out the # of the table returns 0, and as a result, breaks math.random()
error:

01:07:26.246  ServerScriptService.Data_Main:437: invalid argument #2 to 'random' (interval is empty)  -  Server - Data_Main:437

Im really confused by this.
image

You can’t get the length of a table with non numerical indexes in that way. It would only count elements encountered till the first non nil entry (or anything with a numerical index, in sequence, starting at the first index). Get the length a different way; for example, iterate in pairs and increment a counter.

Just removed the names from all the tables and it works fine now. I feel dumb

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