Cant get random item from table

local FellaData = {
	Chris = {
		Sound = "rbxassetid://89308058716659",
		Image = "rbxassetid://60812374"
	},
	Boy = {
		Sound = "rbxassetid://5104257032",
		Image = "rbxassetid://14799621400"
	},
	Pleiades = {
		Sound = "rbxassetid://1973317014",
		Image = "rbxassetid://12161861155"
	},
	slungus = {
		Sound = "rbxassetid://3069892996",
		Image = "rbxassetid://17638689908"
	},
	Fart = {
		Sound = "rbxassetid://6811876591",
		Image = "rbxassetid://96087440195115"
	},
	bratz = {
		Sound = "rbxassetid://8057457330",
		Image = "rbxassetid://13563649165"
	}
}



local function ProduceFella()
	local Fella = script.Parent.Parent.Parent.Feller:Clone()
	local ChosenFella = FellaData[math.random(1, #FellaData)]
	print(ChosenFella)
	Fella.Omg.SoundId = ChosenFella.Sound
	Fella.Image = ChosenFella.Image
	return Fella
end

in the “ProduceFella” function, when i call it the part that gets a random table image always returns nil I also tried to use math.random(1, 6) but that didnt work either
This just returns nil

That’s because it’s a dictionary. When you create a table like this:

{1, 2, 3}

It’s actually read in this format:

{
    [1] = 1,
    [2] = 2,
    [3] = 3
}

so, should you use the # operator on this table, it’ll return 3. This is because the # operator looks for the key 1 in the table and then returns the highest counting up number which is a key of the table.
So:

local tbl = {
    [1] = 3,
    [2] = 4,
    [4] = 5
}
print(#tbl) --> 2

local tbl = {
    [2] = 2,
    [3] = 5,
    [7] = 3
}
print(#tbl) --> 0

local tbl = {
    Key = 1,
    Key1 = 2
}

print(#tbl) --> returns 2

Note how the behaviour for non-array tables remains the same - it will always return 0, it’s no different for a dictionary.



To fix this, you need to use a separate table for the keys which is an array structure and then pick the key from there.

local keys = {"Chris", "Boy", "Pleiades"} --etc.

local chosen = FellaData[keys[math.random(1, #keys)]]

Hi! I improved your code

local FellaData = {
	Chris = {
		Index = 1,
		Sound = "rbxassetid://89308058716659",
		Image = "rbxassetid://60812374"
	},
	Boy = {
		Index = 2,
		Sound = "rbxassetid://5104257032",
		Image = "rbxassetid://14799621400"
	},
	Pleiades = {
		Index = 3,
		Sound = "rbxassetid://1973317014",
		Image = "rbxassetid://12161861155"
	},
	slungus = {
		Index = 4,
		Sound = "rbxassetid://3069892996",
		Image = "rbxassetid://17638689908"
	},
	Fart = {
		Index = 5,
		Sound = "rbxassetid://6811876591",
		Image = "rbxassetid://96087440195115"
	},
	bratz = {
		Index = 6,
		Sound = "rbxassetid://8057457330",
		Image = "rbxassetid://13563649165"
	}
}

--We need to add index to table because it's dictionary

local function ProduceFella()
	local Fella = script.Parent.Parent.Parent.Feller:Clone()
	local FellasAmount = 0 --Variable for amount of fellas in table
	local RealFella --Variable for choosen fella
	
	for _, Fellas in pairs(FellaData) do --Get length of table
		FellasAmount += 1
	end
	
	local ChosenFella = math.random(1, FellasAmount) --Position of fella in table
	
	for _, Fellas in pairs(FellaData) do --Searching table for fella
		if Fellas.Index == ChosenFella then
			RealFella = Fellas
		end
	end
	
	Fella.Omg.SoundId = RealFella.Sound
	Fella.Image = RealFella.Image
end

As person upper me said, we cant just get length of dictionary, but we can get it with loop
Sorry for bad English

ok thanks! ill try this. Ive never really needed to get random dictionary entries before, so thanks for helping!

1 Like
local FellaData = {
	Chris = {
		Sound = "rbxassetid://89308058716659",
		Image = "rbxassetid://60812374"
	},
	Boy = {
		Sound = "rbxassetid://5104257032",
		Image = "rbxassetid://14799621400"
	},
	Pleiades = {
		Sound = "rbxassetid://1973317014",
		Image = "rbxassetid://12161861155"
	},
	slungus = {
		Sound = "rbxassetid://3069892996",
		Image = "rbxassetid://17638689908"
	},
	Fart = {
		Sound = "rbxassetid://6811876591",
		Image = "rbxassetid://96087440195115"
	},
	bratz = {
		Sound = "rbxassetid://8057457330",
		Image = "rbxassetid://13563649165"
	}
}

function PickRandom()
	local count = 0
	local ChosenKey, ChosenFella
	for key, data in pairs(FellaData) do count += 1
		if math.random(count) == 1 then
			ChosenKey, ChosenFella = key, data
		end
	end

	return ChosenKey, ChosenFella
end

local pickedKey, picked = PickRandom()

print(pickedKey)
print(picked.Sound)
print(picked.Image)

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