math.randomseed(tick()*os.time()*math.random(1, 1000))
local function attemptPurchase(player, cost)
if player:WaitForChild("ValuesFolder"):WaitForChild("Credits") then
if (player.ValuesFolder.Credits.Value - cost > player.ValuesFolder.Credits.Value) or player.ValuesFolder.Credits.Value - cost < 0 then
return false
else
player.ValuesFolder.Credits.Value = player.ValuesFolder.Credits.Value - cost
return true
end
else
return false
end
end
local function GetRarity(itemname)
for i,v in pairs(knifes) do
for x,d in pairs(v) do
if x == itemname then
return i
end
end
end
end
local function GetColor(rarity)
if rarity == "Common" then
return Color3.fromRGB(197, 197, 197)
elseif rarity == "Uncommon" then
return Color3.fromRGB(121, 189, 89)
elseif rarity == "Rare" then
return Color3.fromRGB(55, 127, 189)
elseif rarity == "Legendary" then
return Color3.fromRGB(189, 137, 34)
elseif rarity == "Unique" then
return Color3.fromRGB(130, 35, 189)
end
end
function RemoteEvents.PurchaseCrate.OnServerInvoke(player, itemname)
local item, cost, chances, items = itemname, nil, nil, nil
for i,v in pairs(shopInfo.Crates) do
if v.Name == item then
cost = v.Price
chances = v.Chances
items = v.Items
break
end
end
if attemptPurchase(player, cost) == true then
local RandomPick = {}
local chose
for i,v in pairs(chances) do
if item == "Basic Crate" then
for x = 1, v/.001 do
table.insert(RandomPick, i)
end
end
end
for i = 1,30 do
local chosenRarity, chosenItem = RandomPick[math.random(1, #RandomPick)], nil
local possibleItems, randomizedItems = {}, {}
for x,d in pairs(items) do
if GetRarity(d) == chosenRarity then
table.insert(possibleItems, d)
end
end
repeat
local randomItem = possibleItems[math.random(1, #possibleItems)]
table.insert(randomizedItems, randomItem)
for i,v in pairs(possibleItems) do
if v == randomItem then
table.remove(possibleItems, i)
break
end
end
until #possibleItems == 0
chosenItem = randomizedItems[math.random(1, #randomizedItems)]
game.ReplicatedStorage.RemoteEvents.CreateUnboxSlot:FireClient(player, ("Frame" .. tostring(i)), chosenRarity, chosenItem)
if i == 22 then
chose = chosenItem
if player:WaitForChild("ValuesFolder"):WaitForChild("SaveData").Value == true then
local ValuesKey = "ValuesKey_5#User_" .. player.UserId
local function save()
local values = ValuesStore:GetAsync(ValuesKey)
table.insert(values.Inventory.Knifes, chosenItem)
ValuesStore:SetAsync(ValuesKey, values)
end
local tries = 0
repeat
local success,message = pcall(save)
tries = tries + 1
if not success then
if tries == 5 then
player.ValuesFolder.Credits.Value = player.ValuesFolder.Credits.Value + cost
return "Error"
end
wait(.1)
end
until success or tries == 5
else
player.ValuesFolder.Credits.Value = player.ValuesFolder.Credits.Value + cost
return "Error"
end
end
end
return chose
else
return false
end
end
Take note that this IS a Server Script and my problem is that when ran, the unboxing is randomized, however it has a pattern that if you will get the same exact items in the same order if you unbox in any servers, help is appreciated! Thanks!
