Implementing Color Rarity Table

I have this code:

local cubes = {}
local lastCubeSpawnTime = 0
local cubeSpawnInterval = 1 -- seconds

local CubeRarity = {
 
	{Name = "Name", Color = Color3.fromRGB(255, 255, 240), Rarity = 5},
	{Name = "Name", Color = Color3.fromRGB(255, 240, 255), Rarity = 5},
	{Name = "Name", Color = Color3.fromRGB(240, 255, 255), Rarity = 5},
	{Name = "Name", Color = Color3.fromRGB(255, 255, 225), Rarity = 6},    
         ...5822 other entries of colors.
	{Name = "Name", Color = Color3.fromRGB(0, 0, 30), Rarity = 53},
	{Name = "Name", Color = Color3.fromRGB(15, 0, 0), Rarity = 54},
	{Name = "Name", Color = Color3.fromRGB(0, 15, 0), Rarity = 54},
	{Name = "Name", Color = Color3.fromRGB(0, 0, 15), Rarity = 54},
	{Name = "Name", Color = Color3.fromRGB(0, 0, 0), Rarity = 55},
}

local ShapeRarity = {}

local MaterialRarity = {}

local SizeRarity = {}

while true do
	if tick() - lastCubeSpawnTime >= cubeSpawnInterval then
		lastCubeSpawnTime = tick()
		local cube = Instance.new("Part")
		cube.Name = "White Cube" 
		cube.Size = Vector3.new(4, 4, 4)
		cube.Position = Vector3.new(math.random(-250, 250), 1000, math.random(-250, 250))
		cube.Anchored = false
		cube.Parent = game.Workspace
		table.insert(cubes, cube)

		game:GetService("Debris"):AddItem(cube, 60)
	end
	wait()
end

This is for a RNG game I’m making on ROBLOX, where Classic White Cube is the base 1/1 rarity. The tables are Rarity Categories. For the base rarity, White is the 1/1 color rarity, Classic is the 1/1 material rarity, and cube is the 1/1 shape rarity, and 4x4x4 is the 1/1 size rarity but White, Classic, Cube, and 4x4x4 must not go in the table only the other rarities must go there. For each ColorRarity entry (The one we are focusing on), the name is the color name, the color is the RGB values, and the rarity value is O for the formula 1/O. O means how many objects have spawned. Let’s say an unspecified color has Rarity value of 15, that means for every 15 objects that fall from the sky, one of those objects has that unspecified color. I have tried this:

}

local function getRandomColor()
    local totalWeight = 0
    local weights = {}

    for _, entry in ipairs(CubeRarity) do
        local weight = 1 / entry.Rarity
        totalWeight = totalWeight + weight
        table.insert(weights, {entry = entry, cumulative = totalWeight})
    end

    local randomPick = math.random() * totalWeight

    for _, weightData in ipairs(weights) do
        if randomPick <= weightData.cumulative then
            return weightData.entry
        end
    end

    return CubeRarity[1]
end

while true do
    if tick() - lastCubeSpawnTime >= cubeSpawnInterval then
        lastCubeSpawnTime = tick()
        totalCubesSpawned += 1

        local colorEntry = getRandomColor()

        local cube = Instance.new("Part")
        cube.Name = colorEntry.Name .. " Cube"
        cube.Size = Vector3.new(4, 4, 4)
        cube.Position = Vector3.new(math.random(-250, 250), 1000, math.random(-250, 250))
        cube.Color = colorEntry.Color
        cube.Anchored = false
        cube.Parent = game.Workspace
        table.insert(cubes, cube)

        game:GetService("Debris"):AddItem(cube, 60)
    end
    wait()
end

But that didn’t seem right to me.

1 Like

Hey, could you explain exactly what the issue you are having is? If you need help designing a system you could explain exactly what you want and what goals you have, but it seems like you have that down roughly. When you are posting, it’s your job to provide a clear question or state the specific errors you are getting, not the assistants. I know quite a bit about statistics, but I’m not understanding exactly what you are going for.

The problem is that when I run the program I either have all white cubes or no white cubes at all.

My English is not good, but from what I understand you are saying is that you are getting the same rarity. From my observation, the problem is in the way the table was stored. It is inside the function, being reset every time a random color is called. Shouldn’t this be as a global variable?

Edit1: When I say global variable I am referring to a variable that the entire script can read, not just a part of the code.

A module I use is Chance by @avodey!

You can input all the chances and then add luck modifiers (which you probably wont need) but you should be able to easily use this to implement your system! :slight_smile:

1 Like

Oh wow thanks! I didn’t know there was a fully made kit.