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.