local Types = { ["LeftPunch"] = {Id = 1784490345, DamagePart = "LeftHand", Damage = 10}, ["RightPunch"] = {Id = 1784483797, DamagePart = "RightHand", Damage = 10} }
local Type = Types[math.random(1, #table)] print(Type)
it keep saying interval is empty
I did some reading up on why you can't print(Types[1])
. Or print(#Types)
keeps returning 0. Here's a quote from:
The # operator doesn't count all the items in the table (!). Instead it finds the last integer (non-fractional number) key. Because of how it's implemented its results are undefined if all the integer keys in the table aren't consecutive. Which is why it shouldn't be used for tables used as sparse arrays[2]).
If you go to the website and click the "2" it will lead you to Wikipedia and you can read more about it.
Interval is empty because you tried to do math.random(1,0)
Not sure if this is the best approach but you can explicitly assign an Index value in your tables.
01 | -- Assign your own index |
02 | local Types = { |
03 | [ "LeftPunch" ] = { Index = 1 , Id = 1784490345 , DamagePart = "LeftHand" , Damage = 10 } , |
04 | [ "RightPunch" ] = { Index = 2 , Id = 1784483797 , DamagePart = "RightHand" , Damage = 10 } |
05 | } |
06 | local numOfPunches = 0 |
07 |
08 | print ( "What is #table: " .. #table) |
09 | --print(math.random(1,0)) -- Interval is empty |
10 | --print(math.random(1,#table)) -- Interval is empty |
11 |
12 | print (#Types) -- Prints 0 |
13 | -- Not what we wanted |
14 | print (Types [ 1 ] ) -- Prints nil |
15 | -- Also not what we wanted |
01 | local Types = { |
02 | Enabled = true |
03 |
04 | ,RightPunch = { |
05 | Enabled = true |
06 |
07 | ,Damage = 10 |
08 |
09 | ,Id = 1784483797 |
10 | ,Hand = "RightHand" |
11 | } |
12 |
13 | ,LeftPunch = { |
14 | Enabled = true |
15 |
Here is a generalized WeightedProbability Generator function. It is also installable through the RoStrap plugin. Documentation available here.
01 | local WeightedProbabilities = { } |
02 |
03 | local WeightedProbabilityFunction = { |
04 | -- Generates option-picker functions from relative probabilities |
06 | -- @rostrap WeightedProbabilityFunction |
07 | -- @author Validark |
08 |
09 | new = function (ProbabilityData) |
10 | -- @param dictionary in the form: { |
11 | -- [variant option1] = number Weight1; |
12 | -- [variant option2] = number Weight2; |
13 | -- } |
14 | -- @returns a function that, when called, selects an option from its probability and returns it |
15 |