Why does it repeat the same color even though I have checks?


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
image

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

image

In case anyone skims over and doesn’t see, this is the rarity checks I have

Struggling hard over here, please tell me someone has any sort of idea.

Instead of using if then over and over, use elseif statements instead. Kinda like this:

if Value == 1 then
print(“1”)
elseif Value == 2 then
print(“2”)
end

I’m on phone right now, so it would be harder to actually edit the already existing code, sorry lol.

I appreciate the attempt, but the code would just repeat the current statements I already have, I tried it and it’s still the same issue of repeating the same color which what I need fixed
image

Although it did help me realize something, the background is the color rarity of the ending product
So it will find the selected item’s rarity then change all boxes to that color. I just don’t know how I would be able to change the other boxes, I realize this isn’t a problem but I also feel as if it ruins the fun of the spinning process if you know you got a rarity level 1 // gray

It looks like the problem is that you aren’t using item, instead you keep using chosenItem. Try replacing this line:
local rarity = chosenItem.Rarity.Value
with this:
local rarity = item.Rarity.Value

1 Like

Can’t believe it was that simple haha, thank you so much for helping me fix this.
image

1 Like