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 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