How do I randomise a dictionary containing functions?

I have a table which contains a list of functions and I want to randomise the order of each value but using pairs DOES NOT WORK

I could use a randomising function to randomise the order of the table (theres a bunch online) but they rely on grabbing each value’s integer number and my table doesn’t have an integer for each function as it goes like this:

{
   ["FFA"] = "function",
   ["Nothing happening"] = "function",
   ["Teamed"] = "function"
}

Can someone help me with this issue?

1 Like

Are you trying to randomize the values to other keys in the table randomly? Your explanation is a little confusing

tables are unordered and random already by default. Are you trying to just select a random key in that table? Or what are you trying to do?

yes I am trying to select a random key in the table but roblox only allows integers in math.random sorry for the confusing explanation

Tables (i.e. arrays) do have order.
Dictionaries (like in this person’s use case) do not.


In order to get a random function from the dictionary, you will need to use a for loop to get all the keys, then get a random key from the keys.

For example:

local Dict = {
   ["FFA"] = "function",
   ["Nothing happening"] = "function",
   ["Teamed"] = "function"
}

local Keys = {}
for key, _ in pairs(Dict) do
    table.insert(Keys, key)
end

local RandomKey = Keys[math.random(#Keys)]
local Function = Dict[RandomKey]
Function()

ok ill try that and see if it works

Update: thank you it does

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