# Trouble with chance system

Hey!

I have been making a chance system for my shop but it’s not really working. By this I mean that I get super rare cards 90% of the time.

Script:

``````module.GetCard = function(pack)

local ChanceTable = {}

for i,v in pairs(pack) do

local chance = 0

if string.find(v, "S1") then
chance += prices.Prices["S1"]
elseif string.find(v, "S2") then
chance += prices.Prices["S2"]
elseif string.find(v, "S3") then
chance += prices.Prices["S3"]
elseif string.find(v, "S4") then
chance += prices.Prices["S4"]
elseif string.find(v, "S5") then
chance += prices.Prices["S5"]
end

table.insert(ChanceTable, {[v] = chance})

end

local Weight = 0

for _, Chance in pairs(ChanceTable) do
for _, value in pairs(Chance) do
Weight += (value * 10)
break
end
end
print(ChanceTable)

local ranNumber = math.random(1, Weight)

Weight = 0
for prize, chance in pairs(ChanceTable) do

for _, value in pairs(chance) do
Weight += (value * 10)
end

if Weight >= ranNumber then
for j,k in pairs(chance) do
return j
end
end

end

end
``````

When I print chance table this is what is printed:

`````` {
[1] =  ▼  {
["Kainu S4"] = 160
},
[2] =  ▼  {
["Blackmoustache S3"] = 80
},
[3] =  ▼  {
["Taro S2"] = 40
},
[4] =  ▼  {
["Loof S2"] = 40
},
[5] =  ▼  {
["Diu S5"] = 320
}
}
``````
2 Likes

Don’t break out of the loop:

``````local Weight = 0

for _, Chance in pairs(ChanceTable) do
for _, value in pairs(Chance) do
Weight += value
end
end
``````

Hey! Thanks for the help but this doesn’t seem to be working

Sorry, try this

``````local Weight = 0

for _, Chance in pairs(ChanceTable) do
for _, value in pairs(Chance) do
Weight += value * 10
end
end
``````

Hey, sorry that’s what I tried

I solved it. I was doing “chance += prices.Prices” when it should have been “chance += PacksModule.Chances”. That’s what I get for copying stuff over lol

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