Trying to get a random item from a dictionary

local traits = {
	 powerful = "Powerful",
	 manic = "Manic",
	insane = "Insane",
	motivational = "Motivational",
	 inspiring =  "Inspirational",
	 Audacious = "Audacious",
	Brave = "Brave",
	Arrogant = "Arrogant",
	Emotional  = "Emotional",
	Intelligent = "Intelligent",
	Unintelligent = "Unintelligent",
	
}

local nations  = script.Parent:GetChildren()

for i,nation in pairs(nations) do
	print(nation)
	if nation:IsA("Folder") and not nation:IsA("Script") then
		local Leader = nation:FindFirstChild("Leader")
		
		Leader.Traits.Value = next(traits) 
		end
	end

I’m trying to get a random trait from the dictionary above, since math.random doesn’t work for dictionaries, i used next() but that only chooses one trait from the dictionary instead of randomly choosing different traits for different leaders.

1 Like

Probably this is not the best option, but I would do this:

local traits = {
	powerful = "Powerful",
	manic = "Manic",
	insane = "Insane",
	motivational = "Motivational",
	inspiring =  "Inspirational",
	Audacious = "Audacious",
	Brave = "Brave",
	Arrogant = "Arrogant",
	Emotional  = "Emotional",
	Intelligent = "Intelligent",
	Unintelligent = "Unintelligent",

}

local TableToCount = {}

for k, t in pairs(traits) do
	table.insert(TableToCount, k)
end

warn(traits[TableToCount[math.random(0, #TableToCount)]])

It warns a random trait inside that dictionary everytime the warn is called

If you are trying to get a random word, you don’t need a dictionary. Make it a table instead e.g.

local traits = {"Powerful", "Manic", "Insane"}
local chosenTrait = traits[math.random(#traits)]

I plan on giving them a value later, so it isn’t just a random word

Without knowing what you are trying to actually do with it, I can’t provide the best solution.
The answer @Dev_Peashie gave should work for what you want

1 Like

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