Unboxxing System ISSUE

Hello guys! I am trying to fix this script to make the unboxxing system GUI work when I open a crate, but unfortunately during the unboxxing phase I can’t see the image of the weapons, I’ve tried a lot of methods, like fixxing the camera but it doesn’t work.

Can you help me?

this is the script:

– Variables
local rs = game:GetService(“ReplicatedStorage”) – Access ReplicatedStorage service
local remotes = rs:WaitForChild(“RemoteEvents”) – Remote events container
local rarityProperties = rs:WaitForChild(“RarityProperties”) – Rarity properties
local items = rs:WaitForChild(“Items”) – Items container
local crates = rs:WaitForChild(“Crates”) – Crates container

– GUI for the crate shop
local shopGui = script.Parent:WaitForChild(“CrateShopGui”) – The crate shop GUI
shopGui.Enabled = true – Make sure the shop GUI is visible
local openShopBtn = shopGui:WaitForChild(“OpenButton”) – Button to open the shop
openShopBtn.Visible = true – Make the button visible
local shopFrame = shopGui:WaitForChild(“CrateShopFrame”) – The main frame of the shop
shopFrame.Visible = false – Initially hide the shop frame
local closeShopBtn = shopFrame:WaitForChild(“CloseButton”) – Button to close the shop
local cratesList = shopFrame:WaitForChild(“CratesList”) – List of crates in the shop
local selectedCrate = shopFrame:WaitForChild(“SelectedCrate”) – Frame for the selected crate
selectedCrate.Visible = false – Hide it initially

– GUI for the opened crate
local openedGui = script.Parent:WaitForChild(“OpenedCrateGui”) – The crate opened GUI
openedGui.Enabled = false – Initially, the opened crate GUI is not visible
local openedFrame = openedGui:WaitForChild(“CrateFrame”) – Frame for opened crate
openedFrame.Visible = false – Hide the opened crate frame
local closeOpenedBtn = openedFrame:WaitForChild(“ContinueButton”) – Button to continue after opening the crate
local openedItemsFrame = openedFrame:WaitForChild(“ItemsFrame”) – Frame for displaying opened items

– Templates for crate buttons and selected items
local crateButtonTemplate = script:WaitForChild(“CrateShopButton”) – Template for crate buttons
local selectedCrateItemTemplate = script:WaitForChild(“SelectedCrateItemFrame”) – Template for selected crate items
local openingCrateItemTemplate = script:WaitForChild(“OpeningCrateItemFrame”) – Template for opening crate items

local rnd = Random.new() – Random number generator

– Open and close buttons functionality
openShopBtn.MouseButton1Click:Connect(function() – When the “open shop” button is clicked
if openedFrame.Visible == false then
shopFrame.Visible = not shopFrame.Visible – Toggle the shop visibility
end
end)

closeShopBtn.MouseButton1Click:Connect(function() – When the “close shop” button is clicked
shopFrame.Visible = false – Hide the shop frame
end)

closeOpenedBtn.MouseButton1Click:Connect(function() – When the “continue” button is clicked after opening a crate
openedFrame.Visible = false – Hide the opened frame
openedGui.Enabled = false – Disable the opened crate GUI

-- Destroy all item frames in the opened items frame
for _, child in pairs(openedItemsFrame.ItemsContainer:GetChildren()) do
    if child:IsA("Frame") then
        child:Destroy()
    end
end

end)

– Setting up crates in the shop
local crateButtons = {}

for _, crate in pairs(crates:GetChildren()) do – Iterate through all crates
local crateProperties = require(crate) – Get the properties of the crate

local newBtn = crateButtonTemplate:Clone()  -- Clone the crate button template
newBtn.Name = crate.Name  -- Set the button name to the crate's name
newBtn.CrateName.Text = crate.Name  -- Set the button text to the crate's name
newBtn.CrateImage.Image = crateProperties["Image"]  -- Set the image of the crate

-- Functionality for when a crate button is clicked
newBtn.MouseButton1Click:Connect(function()
    if selectedCrate.CrateName.Text ~= crate.Name then  -- If the crate is not already selected
        selectedCrate.CrateName.Text = crate.Name  -- Set the selected crate's name
        selectedCrate.CrateImage.Image = crateProperties["Image"]  -- Set the selected crate's image
        selectedCrate.UnboxButton.Text = "$" .. crateProperties["Price"]  -- Set the price of the crate
        
        -- Display the chances of each rarity for this crate
        local rarities = {}
        for rarityName, chance in pairs(crateProperties["Chances"]) do
            table.insert(rarities, {rarityName, chance})
        end
        -- Sort the rarities by order
        table.sort(rarities, function(a, b)
            return rarityProperties[a[1]].Order.Value < rarityProperties[b[1]].Order.Value
        end)
        
        -- Create the text that will display the chances
        local raritiesText = ""
        for _, rarity in pairs(rarities) do
            local color = rarityProperties[rarity[1]].Color.Value
            color = {R = math.round(color.R * 255), G = math.round(color.G * 255), B = math.round(color.B * 255)}  -- Convert RGB to integer values
            raritiesText = raritiesText .. '<font color="rgb(' .. color.R .. ',' .. color.G .. ',' .. color.B .. ')">' .. rarity[1] .. ': <b>' .. rarity[2] .. '%</b></font><br />'
        end
        selectedCrate.RaritiesText.RichText = true  -- Enable rich text formatting
        selectedCrate.RaritiesText.Text = raritiesText  -- Display the rarity chances in the selected crate frame
        
        -- Clean the previous items list in the selected crate
        for _, child in pairs(selectedCrate.ItemsList:GetChildren()) do
            if child:IsA("Frame") then
                child:Destroy()  -- Destroy each item frame
            end
        end
        
        -- Sort the unboxable items based on their rarity order
        local unboxableItems = crateProperties["Items"]
        table.sort(unboxableItems, function(a, b)
            return 
                (rarityProperties[items:FindFirstChild(a, true).Parent.Name].Order.Value < rarityProperties[items:FindFirstChild(b, true).Parent.Name].Order.Value)
                or (rarityProperties[items:FindFirstChild(a, true).Parent.Name].Order.Value == rarityProperties[items:FindFirstChild(b, true).Parent.Name].Order.Value)
                and (a < b)
        end)
        
        -- Display the unboxable items in the UI
        for _, unboxableItemName in pairs(unboxableItems) do
            local newItemFrame = selectedCrateItemTemplate:Clone()
            newItemFrame.ItemName.Text = unboxableItemName  -- Set the item name
            newItemFrame.ItemName.TextColor3 = rarityProperties[items:FindFirstChild(unboxableItemName, true).Parent.Name].Color.Value  -- Set the item color based on rarity
            
            -- Create a model for the item
            local itemModel = Instance.new("Model")
            
            for _, child in pairs(items:FindFirstChild(unboxableItemName, true):GetChildren()) do
                if not (child:IsA("Script") or child:IsA("LocalScript") or child:IsA("ModuleScript") or child:IsA("Sound")) then
                    child:Clone().Parent = itemModel  -- Clone the item and parent it to the model
                end
            end
            
            itemModel:PivotTo(CFrame.new() * CFrame.Angles(math.rad(-39), 0, 0))  -- Adjust item model orientation
            
            itemModel.Parent = newItemFrame.ItemImage  -- Parent the model to the item image container
            
            local currentCamera = Instance.new("Camera")  -- Create a camera to view the model
            currentCamera.CFrame = CFrame.new(Vector3.new(-itemModel:GetExtentsSize().Y * 0.7, 0, 0), itemModel:GetPivot().Position + Vector3.new(0, -0.1, 0))
            currentCamera.Parent = newItemFrame.ItemImage  -- Parent the camera to the item image
            newItemFrame.ItemImage.CurrentCamera = currentCamera  -- Set the camera for the image
            
            newItemFrame.Parent = selectedCrate.ItemsList  -- Parent the item frame to the items list
        end
        
        selectedCrate.Visible = true  -- Make the selected crate visible
    end
end)

table.insert(crateButtons, {newBtn, crateProperties["Price"]})  -- Store the button and price

end

– Sorting crate buttons based on price and name
table.sort(crateButtons, function(a, b)
return (a[2] < b[2]) or (a[2] == b[2] and a[1].Name < b[1].Name)
end)

– Adding crate buttons to the crate list
for _, crateButton in pairs(crateButtons) do
crateButton[1].Parent = cratesList
end

– Buying crates
selectedCrate.UnboxButton.MouseButton1Click:Connect(function()
if selectedCrate.Visible == true and game.Players.LocalPlayer.leaderstats.Cash.Value >= tonumber(string.sub(selectedCrate.UnboxButton.Text, 2, -1)) then
remotes:WaitForChild(“BuyCrate”):FireServer(selectedCrate.CrateName.Text) – Trigger the BuyCrate event
end
end)

– Unboxing crates
function lerp(a, b, t)
return a + (b - a) * t – Linear interpolation function
end

function tweenGraph(x, pow)
x = math.clamp(x, 0, 1) – Clamp x to be between 0 and 1
return 1 - (1 - x)^pow – Apply the curve to x
end

– Event when a crate is opened
remotes:WaitForChild(“CrateOpened”).OnClientEvent:Connect(function(crateName, itemChosen, unboxTime)
local crateProperties = require(crates[crateName]) – Get the crate properties

local numItems = rnd:NextInteger(20, 100)  -- Generate a random number of items to display
local chosenPosition = rnd:NextInteger(15, numItems - 5)  -- Randomly choose a position for the item that will be shown

for i = 1, numItems do
    local rarityChosen = itemChosen.Parent.Name  -- Start by choosing the rarity of the item
    local randomItemChosen = itemChosen  -- Set the initially chosen item to the provided item
    
    if i ~= chosenPosition then  -- For items that are not the chosen one
        local rndChance = rnd:NextNumber() * 100  -- Generate a random chance
        local n = 0
        
        for rarity, chance in pairs(crateProperties["Chances"]) do
            n += chance  -- Accumulate the chances
            if rndChance <= n then  -- Choose a rarity based on the random chance
                rarityChosen = rarity
                break
            end
        end

        -- Choose a random item from the selected rarity
        local unboxableItems = crateProperties["Items"]
        for i = #unboxableItems, 2, -1 do
            local j = rnd:NextInteger(1, i)
            unboxableItems[i], unboxableItems[j] = unboxableItems[j], unboxableItems[i]  -- Shuffle the items
        end
        
        for _, itemName in pairs(unboxableItems) do
            if items:FindFirstChild(itemName, true).Parent.Name == rarityChosen then
                randomItemChosen = items:FindFirstChild(itemName, true)  -- Select the item
                break
            end
        end
    end
    
    -- Create a frame to display the item
    local newItemFrame = openingCrateItemTemplate:Clone()
    newItemFrame.ItemName.Text = randomItemChosen.Name
    newItemFrame.ItemName.TextColor3 = rarityProperties[rarityChosen].Color.Value  -- Set the item name color
    
    -- Create an ImageLabel for the item's texture (if it has one)
    local textureLabel = Instance.new("ImageLabel")
    textureLabel.Size = UDim2.new(1, 0, 0.5, 0)  -- Set the size
    textureLabel.Position = UDim2.new(0, 0, 0, 0)  -- Set the position
    textureLabel.BackgroundTransparency = 1  -- Make it transparent
    textureLabel.BorderSizePixel = 0  -- Remove borders
    
    -- Check if the item has a Tool and assign its texture (if available)
    local weaponTool = randomItemChosen.Parent:FindFirstChildOfClass("Tool")
    if weaponTool then
        if weaponTool:FindFirstChild("TextureId") then
            textureLabel.Image = weaponTool.TextureId  -- Set the texture from the Tool's TextureId
        else
            textureLabel.Image = "rbxassetid://defaultTextureId"  -- Use a default texture if none exists
        end
    else
        textureLabel.Image = "rbxassetid://defaultTextureId"  -- Fallback texture
    end
    
    -- Update the image and make it visible
    textureLabel.ImageTransparency = 0
    textureLabel.Visible = true
    
    -- Add the texture to the item
    textureLabel.Parent = newItemFrame.ItemImage
    
    -- Keep the item model invisible
    local itemModel = Instance.new("Model")
    itemModel.Parent = newItemFrame.ItemImage
    if itemModel:FindFirstChildWhichIsA('MeshPart') then
        itemModel:FindFirstChildWhichIsA('MeshPart').Transparency = 1  -- Make the mesh part invisible
    end
    
    -- Add the item to the opened crate UI
    newItemFrame.Parent = openedItemsFrame.ItemsContainer
end

openedItemsFrame.ItemsContainer.Position = UDim2.new(0, 0, 0.5, 0)  -- Center the items

local cellSize = openingCrateItemTemplate.Size.X.Scale  -- Calculate the size of each item
local padding = openedItemsFrame.ItemsContainer.UIListLayout.Padding.Scale  -- Get the padding between items
local pos1 = 0.5 - cellSize / 2  -- Starting position
local nextOffset = -cellSize - padding  -- Calculate the offset between items

local posFinal = pos1 + (chosenPosition - 1) * nextOffset  -- Final position of the selected item
local rndOffset = rnd:NextNumber(-cellSize / 2, cellSize / 2)  -- Add some randomness to the final position
posFinal += rndOffset

local timeOpened = tick()  -- Get the current time

openedFrame.CrateName.Text = crateName  -- Display the crate's name
shopFrame.Visible = false  -- Hide the shop frame
closeOpenedBtn.Visible = false  -- Hide the "continue" button initially
openedFrame.Visible = true  -- Make the opened frame visible
openedGui.Enabled = true  -- Enable the opened crate GUI

local pow = rnd:NextNumber(2, 10)  -- Randomize the speed of the unboxing animation
local lastSlot = 0  -- Variable to track the last slot number

while true do  -- Start the animation loop
    local timeSinceOpened = tick() - timeOpened  -- Calculate how much time has passed
    local x = timeSinceOpened / unboxTime  -- Calculate the normalized time (between 0 and 1)
    
    local t = tweenGraph(x, pow)  -- Apply the easing function
    local newXPos = lerp(0, posFinal, t)  -- Interpolate the position of the items
    
    local currentSlot = math.abs(math.floor((newXPos + rndOffset) / cellSize)) + 1  -- Calculate the current slot
    if currentSlot ~= lastSlot then  -- If we have moved to a new slot
        script.TickSound:Play()  -- Play the tick sound
        lastSlot = currentSlot  -- Update the last slot
    end
    
    openedItemsFrame.ItemsContainer.Position = UDim2.new(newXPos, 0, 0.5, 0)  -- Update the position of the items
    
    if x >= 1 then  -- If the animation is complete, stop
        break
    end
    
    game:GetService("RunService").Heartbeat:Wait()  -- Wait for the next frame
end

closeOpenedBtn.Visible = true  -- Show the "continue" button

end)