I don’t know how to sort my dev products.
Here is the script:
local Products = {
bricks100 = {
name = "100 Bricks",
id = 1531409016,
image = "rbxassetid://157942893",
price = "10 R$"
},
bricks500 = {
name = "500 Bricks",
id = 1531410123,
image = "rbxassetid://157942893",
price = "50 R$"
},
bricks1000 = {
name = "1000 Bricks",
id = 1531410705,
image = "rbxassetid://157942893",
price = "100 R$"
},
bricks5000 = {
name = "5000 Bricks",
id = 1531411372,
image = "rbxassetid://157942893",
price = "500 R$"
},
bricks10k = {
name = "10K Bricks",
id = 1531411833,
image = "rbxassetid://157942893",
price = "1000 R$"
},
}
local ProductHolder = script.Parent
local MPS = game:GetService("MarketplaceService")
local plr = game.Players.LocalPlayer
for i,product in pairs(Products) do
local itemTemplate = game.ReplicatedStorage.ItemTemplate:Clone()
itemTemplate.Title.Text = product.name
itemTemplate.ImageButton.Image = product.image
itemTemplate.Price.Text = product.price
itemTemplate.Parent = ProductHolder
itemTemplate.ImageButton.MouseButton1Click:Connect(function()
MPS:PromptProductPurchase(plr,product.id)
end)
itemTemplate.Price.MouseButton1Click:Connect(function()
MPS:PromptProductPurchase(plr,product.id)
end)
end
--function that gets the actual price from your text price(for example 100 R$ -> 100)
function priceTextToNum(text: string): number
return tonumber(text:gsub("%D", ""))
end
--Convert dictionary to array
local arrayProducts = {}
for _, v in pairs(Products) do table.insert(arrayProducts, v) end
--sort the array by price
table.sort(arrayProducts, function(a, b)
return priceTextToNum(a.price) < priceTextToNum(b.price)
end)
--use the array instead(since you aren't even using the i parameter in your code)
for _, product in pairs(arrayProducts) do
--your code
end
did i do it right?
local ProductHolder = script.Parent
local MPS = game:GetService("MarketplaceService")
local plr = game.Players.LocalPlayer
local Products = {
bricks100 = {
name = "100 Bricks",
id = 1531409016,
image = "rbxassetid://157942893",
price = "10 R$"
},
bricks500 = {
name = "500 Bricks",
id = 1531410123,
image = "rbxassetid://157942893",
price = "50 R$"
},
bricks1000 = {
name = "1000 Bricks",
id = 1531410705,
image = "rbxassetid://157942893",
price = "100 R$"
},
bricks5000 = {
name = "5000 Bricks",
id = 1531411372,
image = "rbxassetid://157942893",
price = "500 R$"
},
bricks10k = {
name = "10K Bricks",
id = 1531411833,
image = "rbxassetid://157942893",
price = "1000 R$"
},
}
local arrayProducts = {}
for _, v in pairs(Products) do table.insert(arrayProducts, v) end
--sort the array by name
table.sort(arrayProducts, function(a, b)
return a.name < b.name
end)
for _, product in pairs(arrayProducts) do
local itemTemplate = game.ReplicatedStorage.ItemTemplate:Clone()
itemTemplate.Title.Text = product.name
itemTemplate.ImageButton.Image = product.image
itemTemplate.Price.Text = product.price
itemTemplate.Parent = ProductHolder
itemTemplate.ImageButton.MouseButton1Click:Connect(function()
MPS:PromptProductPurchase(plr,product.id)
end)
itemTemplate.Price.MouseButton1Click:Connect(function()
MPS:PromptProductPurchase(plr,product.id)
end)
end
4Avern
(Avern Hendrix)
May 1, 2023, 12:49pm
#5
Here
local Products = {
bricks100 = {
name = "100 Bricks",
id = 1531409016,
image = "rbxassetid://157942893",
price = 10
},
bricks500 = {
name = "500 Bricks",
id = 1531410123,
image = "rbxassetid://157942893",
price = 50
},
bricks1000 = {
name = "1000 Bricks",
id = 1531410705,
image = "rbxassetid://157942893",
price = 100
},
bricks5000 = {
name = "5000 Bricks",
id = 1531411372,
image = "rbxassetid://157942893",
price = 500
},
bricks10k = {
name = "10K Bricks",
id = 1531411833,
image = "rbxassetid://157942893",
price = 1000
},
}
local sortedProducts = {}
for _, product in pairs(Products) do
table.insert(sortedProducts, product)
end
table.sort(sortedProducts, function(a, b)
return a.price < b.price
end)
local ProductHolder = script.Parent
local MPS = game:GetService(“MarketplaceService”)
local plr = game.Players.LocalPlayer
for i, product in ipairs(sortedProducts) do
local itemTemplate = game.ReplicatedStorage.ItemTemplate:Clone()
itemTemplate.Title.Text = product.name
itemTemplate.ImageButton.Image = product.image
itemTemplate.Price.Text = product.price .. " R$"
itemTemplate.Parent = ProductHolder
itemTemplate.ImageButton.MouseButton1Click:Connect(function()
MPS:PromptProductPurchase(plr, product.id)
end)
itemTemplate.Price.MouseButton1Click:Connect(function()
MPS:PromptProductPurchase(plr, product.id)
end)
end
1 Like
Thank you so much. Now it works
1 Like
system
(system)
Closed
May 15, 2023, 12:54pm
#7
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.