I’m doing an unboxing game where you open crates and you can win (in-game) Limiteds
I wanted to add a rarity system, when it came time to implement it into the unbox script it seems that all backgrounds are one color even though I do checks.
(The color changes every time you spin so I assume it’s taking the last 1 recorded and making all boxes that color) – I don’t know how to fix this
local crates = game.ReplicatedStorage:WaitForChild("Crates")
local gui = script.Parent
local cratesGui = gui:WaitForChild("CratesGui")
local unboxGui = gui:WaitForChild("UnboxGui")
local isOpeningCrate = false
local minTime, maxTime = 5, 7
local minCells, maxCells = 21, 21
local openGui = gui:WaitForChild("OpenCrateGui")
openGui.MouseButton1Click:Connect(function()
if isOpeningCrate then return end
cratesGui.Visible = not cratesGui.Visible
end)
for i, crate in pairs(crates:GetChildren()) do
local crateName = crate.Name
local cratePrice = crate.Price.Value
local clonedTemplate = script:WaitForChild("BuyCrateTemplate"):Clone()
clonedTemplate.CrateInfo.Text = crateName .. "\nPrice: " .. cratePrice
clonedTemplate.Parent = cratesGui:WaitForChild("CratesScroll")
clonedTemplate.MouseButton1Click:Connect(function()
if not isOpeningCrate then
isOpeningCrate = true
local cells = math.random(minCells, maxCells)
local unboxTime = math.random(minTime, maxTime)
game.ReplicatedStorage.CrateRE:FireServer(crateName, unboxTime)
local connection
connection = game.ReplicatedStorage.CrateRE.OnClientEvent:Connect(function(chosenItem)
local itemCell = math.random(7, cells - 7)
local itemVPF
unboxGui.ItemsClipping.Scroller.Size = UDim2.new(0, cells * unboxGui.ItemsClipping.Scroller.AbsoluteSize.Y, 1, 0)
unboxGui.ItemsClipping.Scroller.Position = UDim2.new(0, 0, 0, 0)
unboxGui.ItemsClipping.Scroller:ClearAllChildren()
for i = 1, cells do
local vpf = Instance.new("ViewportFrame")
local item
if i ~= itemCell then
repeat item = crates[crateName]:GetChildren()[math.random(#crates[crateName]:GetChildren())]; wait() until item:IsA("Accessory")
else
item = chosenItem
itemVPF = vpf
end
local rarity = chosenItem.Rarity.Value
local clone = item.Handle:Clone()
clone.Parent = vpf
clone.Anchored = true
local cam = Instance.new("Camera")
cam.Parent = vpf
cam.CFrame = CFrame.new(clone.Position + Vector3.new(0, 0, 3), clone.Position)
vpf.CurrentCamera = cam
--// Rarity Checks Here
if rarity == 5 then
vpf.BackgroundColor3 = Color3.fromRGB(255, 255, 127)
end
if rarity == 4 then
vpf.BackgroundColor3 = Color3.fromRGB(129,2,255)
end
if rarity == 3 then
vpf.BackgroundColor3 = Color3.fromRGB(5,125,223)
end
if rarity == 2 then
vpf.BackgroundColor3 = Color3.fromRGB(5,225,123)
end
if rarity == 1 then
vpf.BackgroundColor3 = Color3.fromRGB(124, 124, 124)
end
--//
vpf.Size = UDim2.new(0, unboxGui.ItemsClipping.Scroller.AbsoluteSize.X / cells, 1, 0)
vpf.Position = UDim2.new(0, vpf.AbsoluteSize.X * (i - 1), 0, 0)
vpf.Parent = unboxGui.ItemsClipping.Scroller
end
cratesGui.Visible = false
unboxGui.Visible = true
local offset = math.random(-itemVPF.AbsoluteSize.X/2, itemVPF.AbsoluteSize.X/2)
local distance = (itemVPF.AbsolutePosition.X + (itemVPF.AbsoluteSize.X / 2)) - unboxGui.Marker.AbsolutePosition.X
unboxGui.ItemsClipping.Scroller:TweenPosition(UDim2.new(0, -distance + offset, 0, 0), "InOut", "Quint", unboxTime)
wait(unboxTime)
isOpeningCrate = false
unboxGui.Visible = false
connection:Disconnect()
end)
end
end)
end