Hi, as in my last post, i try to make that when sb buys a crate will recieve a item by chance.
The problem is that when i click sometimes none of the checks are true or the last check is true but throws error saying that i try to index “Chance” with nil “itemsInCrate[i+1].Chance.Value”
game.ReplicatedStorage.Events.BoughtCrate.OnClientEvent:Connect(function(cratename, category)
local crate = game.ReplicatedStorage.Items[category].Crates[cratename]
openingHud.Frame.Crate.Image = crate.Picture.Image
itemsInCrate = crate[category]:GetChildren()
table.sort(itemsInCrate,
function(a,b)
return a.Chance.Value > b.Chance.Value
end
)
local randomnumber = math.random(0, 100)
local itemsPreselected = {}
print(tostring(randomnumber))
for i, v in ipairs(itemsInCrate) do -- These checks
print(tostring(i))
if 100.0 <= randomnumber and randomnumber <= v.Chance.Value and i == 1 then
print("the number is between 100 and "..v.Chance.Value)
table.insert(itemsPreselected, v)
elseif v.Chance.Value <= randomnumber and randomnumber <= 0 and i+1 == #itemsInCrate then
print("the number is between "..v.Chance.Value.."and 0")
table.insert(itemsPreselected, v)
elseif v.Chance.Value <= randomnumber and randomnumber <= itemsInCrate[i+1].Chance.Value then
print("the number is between "..v.Chance.Value.."and "..itemsInCrate[i+1].Chance.Value)
table.insert(itemsPreselected, v)
end
end
local x = #itemsPreselected
local itemSelected = itemsPreselected[math.round(math.random(1, x))]
openingHud.Frame.Preview:ClearAllChildren()
local part = itemSelected.Frame.Preview:Clone()
part.Parent = openingHud.Frame.Preview
part.Position = Vector3.new(0,0,0)
local camera = Instance.new("Camera", openingHud.Frame.Preview)
camera.CFrame = CFrame.new(Vector3.new(0,-8, 1), part.Position)
openingHud.Frame.Preview.CurrentCamera = camera
openTheBox()
end)
The chances of the items from the crate i touched are:
60, 25,15
Edit:
23:21:01.542 Bought crate - Client - LocalScript:4
23:21:01.542 93 - Client - OpenningCrate:144 -- some random number that was returned
23:21:01.542 1 - Client - OpenningCrate:147
23:21:01.543 2 - Client - OpenningCrate:147
23:21:01.543 3 - Client - OpenningCrate:147
23:21:01.543 Players.George_GoreCRAFT.PlayerGui.OpenningCrate:154: attempt to index nil with 'Chance'
Edit2: Nevermind, a post for nothing… i was checking if the randomnumber was higher than 100.0 or the actual chance value instead of doing exactly opposite.