Hello I’m making a code for my shop I want the cloning order is not random
The fix I try:
but the backpack order is so random like this
Example 1
Example 2

How I can fix this?
script.Parent.Visible = false
local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local humanoid = char:WaitForChild("Humanoid")
local camera = workspace.CurrentCamera
local BoughtItems = plr.Leaderstats.PB.Value
local Backpacks = game.ReplicatedStorage:WaitForChild("Backpacks"):GetChildren()
table.sort(Backpacks, function(a, b)
return (a.Pack.Price.Value < b.Pack.Price.Value)
end)
local standTemplate = game.ReplicatedStorage:WaitForChild("StandTemplate")
local itemBoughtRE = game.ReplicatedStorage.Events:WaitForChild("Buy")
local isInShop = false
local PurchasedBacks = plr.Leaderstats.PB
local tweenService, tweenInfo = game:GetService("TweenService"), TweenInfo.new(0.5, Enum.EasingStyle.Quint, Enum.EasingDirection.InOut)
humanoid.Touched:Connect(function(touchedPart)
if touchedPart == workspace.Shop.ShopPad then
if isInShop then return end
isInShop = true
script.Parent.Visible = true
char.HumanoidRootPart.Anchored = true
local currentStand = 1
local function updateBuyBtn()
BoughtItems = plr.Leaderstats.PB.Value
local weapon = Backpacks[currentStand].Pack
local price = weapon.Price.Value
local name = weapon.Orgin.Value
if tonumber(Backpacks[currentStand].Name) > BoughtItems then
script.Parent.BuyButton.BuyText.Text = "Buy "..name.. "for " .. price
script.Parent.Title.Text = name
if tonumber(Backpacks[currentStand].Name) - BoughtItems > 1 then
script.Parent.BuyButton.BuyText.Text = "Please buy previous bags"
script.Parent.Title.Text = name
end
else
script.Parent.BuyButton.BuyText.Text = "ALREADY BOUGHT"
script.Parent.Title.Text = name
end
end
plr.Backpack.ChildAdded:Connect(updateBuyBtn)
local stands = {}
local Bc = game.ReplicatedStorage.Background:Clone()
Bc.Parent = workspace
Bc.Name = "Background"
local IDX = 0
repeat
IDX += 1
local stand = standTemplate:Clone()
stand.Stand.CFrame = stand.Stand.CFrame + Vector3.new(IDX * 5, 0, 0)
stand.CameraPart.CFrame = stand.CameraPart.CFrame + Vector3.new(IDX* 5, 0, 0)
stand.Parent = workspace
table.insert(stands, stand)
local itemClone = Backpacks[IDX].Pack:Clone()
itemClone.Handle.CFrame = stand.Stand.CFrame + Vector3.new(0, 3, 0)
itemClone.Handle.CanCollide = false
itemClone.Parent = stand
local bav = Instance.new("BodyAngularVelocity")
bav.Parent = itemClone.Handle
bav.AngularVelocity = Vector3.new(0, 1, 0)
local bp = Instance.new("BodyPosition")
bp.Parent = itemClone.Handle
bp.Position = itemClone.Handle.Position
until IDX == #Backpacks
local originalCamCFrame = camera.CFrame
camera.CameraType = Enum.CameraType.Scriptable
tweenService:Create(camera, tweenInfo, {CFrame = stands[1].CameraPart.CFrame}):Play()
updateBuyBtn()
script.Parent.LeftArrowButton.MouseButton1Click:Connect(function()
local newStand = currentStand - 1
if newStand > 0 then
currentStand = newStand
updateBuyBtn()
tweenService:Create(camera, tweenInfo, {CFrame = stands[newStand].CameraPart.CFrame}):Play()
end
end)
script.Parent.RightArrowButton.MouseButton1Click:Connect(function()
local newStand = currentStand + 1
if newStand <= #Backpacks then
currentStand = newStand
updateBuyBtn()
tweenService:Create(camera, tweenInfo, {CFrame = stands[newStand].CameraPart.CFrame}):Play()
end
end)
script.Parent.ExitButton.MouseButton1Click:Connect(function()
tweenService:Create(camera, tweenInfo, {CFrame = originalCamCFrame}):Play()
wait(0.5)
camera.CameraType = Enum.CameraType.Custom
char.HumanoidRootPart.Anchored = false
script.Parent.Visible = false
for i, stand in pairs(stands) do
stand:Destroy()
end
workspace.Background:Destroy()
wait(1)
isInShop = false
end)
script.Parent.BuyButton.MouseButton1Click:Connect(function()
local itemBuying = Backpacks[currentStand]
itemBoughtRE:FireServer(itemBuying)
updateBuyBtn()
BoughtItems = plr.Leaderstats.PB.Value
script.Parent.BuyButton.BuyText.Text = "..."
wait(0.2)
updateBuyBtn()
end)
end
end)