okay so the script is in a module script, and i have a table ready inside that module which is
local Classes = {
Hunter = {
ClassNumber = 1,
RunSpeed = 30,
Damage = 10,
Animations = {
RunAnim = "",
PunchAnim = ""
}
},
Tank = {
ClassNumber = 2,
RunSpeed = 23,
Damage = 20,
Animations = {
RunAnim = "",
PunchAnim = ""
}
}
}
-- and when i run this code
local function GetRandomClass()
local Randomizer = math.random(1, #Classes)
for Class, ClassProperties in Classes do
if ClassProperties.ClassNumber == Randomizer then
print("you got... "..tostring(Class))
local Table = {[Class] = ClassProperties}
return Table
end
end
end
the second argument of the Randomizer Variable, doesnt register- Is it because i have multiple tables in one table?
a workaround i did was to just implement a count function
local function Count(Table)
local Num = 0
for i, v in Table do
Num += 1
end
return Num
end
and it worked fine but im wondering if im just being dumb or what
The ‘#’ syntax do not work with dictionaries, it only work with arrays. Therefore, you need to do it differently.
local function GetRandomClass()
local NumberOfClasses = 0
local ClassReferences = {}
for ClassName, ClassInfos in Classes do
NumberOfClasses += 1
ClassReferences[NumberOfClasses] = {tostring(ClassName), Classes[ClassName]}
end
local RandomClass = ClassReferences[math.random(1, NumberOfClasses)]
print("you got the", RandomClass[1], "class with properties:", RandomClass[2])
return RandomClass
end
Thankyou! Works well! But i’m wondering how i can change the Index to something i wanna name it? so like it’s a bit more organized- instead of numbers [1][2] it’ll be [“Name”], [“Stats”] I triedd figuring it out myself but i couldn’t