-
What do you want to achieve? Keep it simple and clear!
I want to draw a random item from my item table. -
What is the issue? Include screenshots / videos if possible!
I cant get it to work; it returns:37686102510
(the weight which I printed) and this:ServerScriptService.modules.items & rarities:36: attempt to compare Random <= number
-
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I’ve found many posts with similar problems but they didn’t seem to help, or maybe I just can’t understand them.
Here’s my script:
-- module def
local M = {}
-- item defs
M.items = {
["Common"] = 2, ["Uncommon"] = 4, ["Good"] = 5, ["Natural"] = 10,
["Rare"] = 16, ["Divinus"] = 32, ["Crystalized"] = 64, ["Rage"] = 128,
["Topaz"] = 150, ["Ruby"] = 350, ["Forbidden"] = 404, ["Emerald"] = 500,
["Gilded"] = 512, ["Ink"] = 700, ["Jackpot"] = 777, ["Sapphire"] = 800,
["Aquamarine"] = 900, ["Wind"] = 912, ["Diaboli"] = 1004, ["Precious"] = 1024,
["Glock"] = 1700, ["Magnetic"] = 2048, ["Glacier"] = 2304, ["Sidereum"] = 4096,
["Bleeding"] = 4444, ["Zolar"] = 5000, ["Pruner"] = 6000, ["Stard"] = 6120,
["Broke"] = 8245, ["Venus"] = 10000, ["Marvs"] = 12000, ["Jupiter"] = 15000,
["FootLettuce"] = 20000, ["Cursed"] = 30000, ["Astronomical"] = 50000,
["Graete"] = 75000, ["Swung"] = 100000, ["Treaded"] = 250000, ["Broken"] = 500000,
["Chakim"] = 1000000, ["Gunks"] = 1500000, ["Mello"] = 5000000, ["Bepsi"] = 10000000,
["NevaGunks"] = 50000000, ["ALBRUKE"] = 100000000, ["Storm"] = 600000000, ["Bellins"] = 1000000000,
["???"] = 2000000000
}
-- this is what im stuck on, i tried to make it so that it would pick a random item with a weighted chance from the items table, but i can't seem to get it to work
function M.drawItem()
-- set the weight and make chance stuff
local weight = 0
for _, chance in pairs(M.items) do
weight += (chance * 10)
end
print(weight)
local ranNumber = Random.new(1, weight)
weight = 0
for item, chance in pairs(M.items) do
weight += (chance * 10)
if weight >= ranNumber then
print(item) -- im just using print for prototype purposes right now
break
end
end
end
return M
Any help would be greatly appreciated.