Is there a way to pick random values from a table that aren't numbers?

Hello, couldn’t you do something like this?

local function getSizeOfDict(dict)
    local count = 0
    for _, _ in pairs(dict)
        count += 1
    end
end

local function getRandom(dict)
    local random = math.random(1, getSizeOfDict(CNRs)]
    local chosen = ""
    for index, _ in pairs(dict) do
        if index == dict[random] then
            chosen = dict[random]
            break
        end
    end
    return chosen
end

for c = 1, 15 do
    displayYourName(getRandom(CNRs))
end

For the displayYourName part, what do I change to make it work correctly?

1 Like

Just change text, whatever it is: textLabel.Text = getRandom(CNRs)

There is no error but it is not spinning, here is the code

local CNRs = {
	--Ult

	Freecss = .05,
	Kurta = .05,
	Zoldyck  = .05,
	Meruem = .05,
	Netero = .05,

	-- Legendary

	Lucilfer = 1,
	Neferpitou = 1,
	Kite = 1,
	Morow = 1,
	Portor = 1,
	Gigante = 1,
	Pyon = 1,

	--Epic

	Uvogin = 3,
	Machi = 3,
	Hazama = 3,
	Shizuku = 3,
	Shalnark = 3,
	Pakunoda = 3,
	Bonolenov = 3,
	Krueger = 3,
	Magcub = 3,
	Mackernasey = 3,
	Nana = 3,

	--Rare

	Knov = 15,
	Hill = 15,
	Yorkshire = 15,
	Paradinight = 15,
	Kobayakawa = 15,
	Bine = 15,
	McMahon = 15,

	--Uncommon

	Siberia = 20,
	Pokkle = 20,
	Hanzo = 20,
	Buhara = 20,
	Baise = 20,
	Bodoro = 20,
	Geretta = 20,

	--Common

	Tsezguerra = 70,
	Agon = 70,
	Amori = 70,
	Umori = 70,
	Imori = 70,
	Tonpa = 70,
	Bourbon = 70,
	Ponzu = 70,
	Kyu = 70,
	Todo = 70,
	Morel = 70,
	Mercer = 70,
	Goz = 70,
}

local function getSizeOfDict(dict)
	local count = 0
	for _, _ in pairs(dict) do
		count += 1
	end
end

local function getRandom(dict)	local random = math.random(1, getSizeOfDict(CNRs))
		local chosen = ""
for index, _ in pairs(dict) do
	if index == dict[random] then
		chosen = dict[random]
		break
	end
end
return chosen
end

for c = 1, 15 do
	script.Parent.Parent.Clan.Text = getRandom(CNRs)
end

Just replace index == dict[random] with index == random

I still need help on this when you are available.

So anyone can get the solution:

local CNRs = {
    --Ult

    Freecss = .05,
    Kurta = .05,
    Zoldyck  = .05,
    Meruem = .05,
    Netero = .05,

    -- Legendary

    Lucilfer = 1,
    Neferpitou = 1,
    Kite = 1,
    Morow = 1,
    Portor = 1,
    Gigante = 1,
    Pyon = 1,

    --Epic

    Uvogin = 3,
    Machi = 3,
    Hazama = 3,
    Shizuku = 3,
    Shalnark = 3,
    Pakunoda = 3,
    Bonolenov = 3,
    Krueger = 3,
    Magcub = 3,
    Mackernasey = 3,
    Nana = 3,

    --Rare

    Knov = 15,
    Hill = 15,
    Yorkshire = 15,
    Paradinight = 15,
    Kobayakawa = 15,
    Bine = 15,
    McMahon = 15,

    --Uncommon

    Siberia = 20,
    Pokkle = 20,
    Hanzo = 20,
    Buhara = 20,
    Baise = 20,
    Bodoro = 20,
    Geretta = 20,

    --Common

    Tsezguerra = 70,
    Agon = 70,
    Amori = 70,
    Umori = 70,
    Imori = 70,
    Tonpa = 70,
    Bourbon = 70,
    Ponzu = 70,
    Kyu = 70,
    Todo = 70,
    Morel = 70,
    Mercer = 70,
    Goz = 70,
}

local function getSizeOfDict(dict)
    local count = 0
    for _, _ in pairs(dict) do
        count = count + 1
    end
    return count
end
local function getRandom(dict)
    local random = math.random(1, getSizeOfDict(dict))
    local count = 0
    for name, _ in pairs(dict) do
        if count == random then
            return name
        end
        count += 1
    end
end

script.Parent.MouseButton1Click:Connect(function()
    for c = 1, 15 do
        script.Parent.Parent.Clan.Text = getRandom(CNRs)
        task.wait(0.06)    
    end
end)

This is probably the simplest way you can do it.

local RandomValue = CNRs[math.random(1, #CNRs)]

This line will always work as long as CNRs has at least one item.

This will only work on arrays, not dicts as the indexes aren’t numbers but string.

oh my apologies I thought it was an array haha!!
It won’t let me put it in the code format but here.

local CNRsKeys = []
for key, value in pairs(CNRs) do
table.insert(CNRsKeys, key)
end
CNRs[CNRsKeys[math.random(1, #CNRsKeys)]]

1 Like

That seems like a possible solution for the problem :white_check_mark:

But mine works too :lying_face::roll_eyes:

1 Like