PieRake
(PieRake)
August 12, 2023, 4:35pm
#1
For the past few hours I’ve been trying to find how get a random item from an array, as the title says. I just haven’t found a single thing that actually works.
And I’ve tried doing table[math.random(1,#table )] and that doesn’t work.
List -
local difficulties = {
easy = {number = 3, text = "Not too bad!", tim = 20},
medium = {number = 6, text = "Kinda hard...", tim = 45},
hard = {number = 12, text = "Really spicy!!!!!!!", tim = 75}
}
That is actually a dictionary and not an array.
You can look at this post for a function that should do the job:
Ah, how convenient. I actually have a library that contains a random key selector for a dictionary. It does what DataErased suggested but invisibly so that you don’t have to make both the dictionary and the array part yourself.
My handy function, DictionarySelectRandom, accounts for arbitrary dictionaries so all you have to worry about is writing out the dictionary. A table of keys will automatically be created and fetched from internally. You just lose the benefit of consistent seeded results …
1 Like
PieRake
(PieRake)
August 12, 2023, 4:43pm
#3
Thank you!! The one you linked me didn’t help, but another comment under the post did.
This is not the same as OP is asking, OP is referring to dictionaries
Example of a dictionary:
local dictionary = {
["Item1"] = game:GetService("ReplicatedStorage").Lily,
["Item2"] = game:GetService("ReplicatedStorage").Rose,
}
As far as getting a random one from the bunch, I believe you’d have to set it up like so:
commons = {
["Lily"] = game:GetService("ReplicatedStorage").Lily,
["Rose"] = game:GetService("ReplicatedStorage").Rose,
["Dandelion"] = game:GetService("Replicated…
system
(system)
Closed
August 26, 2023, 4:43pm
#4
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.