Robloxda bir oyun geliştiriyorum elimde bir adet function var. Bu function içeriisnde bir geliştirme yapmamız lazım
Function updateInventoryGui(player) olarak kullanılıyor bu argümentlere bir yenisi daha eklenmeli oda item_type değeri lakin hala updateInventoryGui(player) olarak kullanıma devam etmeli diğer yerlerde isteğe bağlı olarak bu değer girilirse içerisinde buna göre bir işlem yapılmalı girilmediği taktidrde default nil dönmeli.
bir değer girildiğinde sadece item_info.Type değeri girilen değer ile eşleşen öğelere işlem yapmalı.
Funcvtion kodum şöyle:
function Inventory.updateInventoryGui(player)
-- Rarity öncelik sırası
local rarityOrder = {
Mythical = 1,
Legendary = 2,
Epic = 3,
Rare = 4,
Uncommon = 5,
Common = 6
}
code
Assistant
-- Oyuncunun PlayerGui'sine erişin
local playerGui = player:WaitForChild("PlayerGui")
local menus = playerGui:WaitForChild("Menus")
local inventoryMenu = menus:WaitForChild("InventoryMenu")
local mainFrame = inventoryMenu:WaitForChild("MainFrame")
local scrollingFrame = mainFrame:WaitForChild("ScrollingFrame")
-- Mevcut itemları temizleyin (şablon olan "Item" hariç)
for _, child in ipairs(scrollingFrame:GetChildren()) do
if child.Name ~= "UIGridLayout" and child.Name ~= "ScrollingFrameScript" and child.Name ~= "InventoryHandler" then
-- Eğer isim 'Frame' ise ve Visible özelliği 'false' ise, silme işlemi yapma
if child.Name == "Frame" and child.Visible == false then
-- 'Visible' false olan Frame'leri atla
print("Görünmeyen Frame atlanıyor: " .. child.Name)
else
-- Diğer öğeleri sil
print("Siliniyor: " .. child.Name)
child:Destroy()
end
end
end
-- Şablon olan Item frame'ini bulun
local itemTemplate = scrollingFrame:WaitForChild("Frame")
-- Envanteri ve oyuncu verilerini alın
local inventory = Inventory.getInventory(player) -- Envanteri almak için fonksiyonu kullanın
print("Envanter:", inventory)
-- Oyuncunun EquippedItem listesini alın
local playerData = Inventory.getUserData(player)
local equippedItems = playerData.EquippedItem or {}
-- EquippedItem listesini bir sete dönüştürün
local equippedItemsSet = {}
for equippedItemId, _ in pairs(equippedItems) do
equippedItemsSet[equippedItemId] = true
end
print("Equipped Items Set:", equippedItemsSet)
-- Toplam item sayısını hesaplayın
local totalItems = 0
for _ in pairs(inventory) do
totalItems = totalItems + 1
end
-- Toplam item sayısını UI'da gösterin
local statsBackground = mainFrame:WaitForChild("StatsBackground")
local inventorySpace = statsBackground:WaitForChild("InventorySpace")
local PetsSpace = statsBackground:WaitForChild("PetsSpace")
local totalItemsLabel = inventorySpace:WaitForChild("TextLabel")
local totalPetsLabel = PetsSpace:WaitForChild("TextLabel")
totalItemsLabel.Text = tostring(totalItems)
-- Envanterinizi bir diziye dönüştürün
local inventoryArray = {}
for itemId, itemData in pairs(inventory) do
table.insert(inventoryArray, {id = itemId, data = itemData})
end
-- Diziyi belirlenen sıralama kurallarına göre sıralayın
table.sort(inventoryArray, function(a, b)
local itemInfoA = ItemsConfig:GetItem(a.data.item_type)
local itemInfoB = ItemsConfig:GetItem(b.data.item_type)
local isEquippedA = equippedItemsSet[a.id] or false
local isEquippedB = equippedItemsSet[b.id] or false
-- 1. Öncelik: EquippedItem kontrolü
if isEquippedA ~= isEquippedB then
return isEquippedA -- A true ise ve B false ise, A önce gelir
end
-- 2. Öncelik: Harvest_Gain değerine göre sıralama
local harvestGainA = itemInfoA.Harvest_Gain or 0
local harvestGainB = itemInfoB.Harvest_Gain or 0
if harvestGainA ~= harvestGainB then
return harvestGainA > harvestGainB
end
-- 3. Öncelik: Rarity sırasına göre sıralama
local rarityA = itemInfoA.Rarity or "Common"
local rarityB = itemInfoB.Rarity or "Common"
local orderA = rarityOrder[rarityA] or 6 -- "Common" varsayılan
local orderB = rarityOrder[rarityB] or 6
if orderA ~= orderB then
return orderA < orderB
end
-- 4. Öncelik: item_unix değerine göre sıralama (en yeni önce)
local unixTimeA = a.data.item_unix or 0
local unixTimeB = b.data.item_unix or 0
return unixTimeA > unixTimeB -- Daha yeni item'lar daha büyük unix zamanı
end)
-- Her bir item için UI öğesi oluşturun (sıralanmış dizi kullanılarak)
local total_pets_count = 0;
for _, item in ipairs(inventoryArray) do
local itemId = item.id
local itemData = item.data
local item_id = itemData.item_type
local item_info = ItemsConfig:GetItem(item_id)
local newItem = itemTemplate:Clone()
newItem.Parent = scrollingFrame
newItem.Name = itemId
newItem.Visible = true
-- Statlar
local HarvestGain_stat = newItem:FindFirstChild("stats1")
local FarmIncome_stat = newItem:FindFirstChild("stats2")
local HarvestSpeed_stat = newItem:FindFirstChild("stats3")
if item_info.Type == "scythe" then
HarvestGain_stat.Visible = true
HarvestSpeed_stat.Visible = true
local nameLabel = newItem:FindFirstChild("Frame"):FindFirstChild("icons"):FindFirstChild("Name")
local HarvestGain = HarvestGain_stat:FindFirstChild("Stat1")
local HarvestSpeed = HarvestSpeed_stat:FindFirstChild("Stat3")
if nameLabel and nameLabel:IsA("TextLabel") then
nameLabel.Text = item_info.Name
end
if HarvestGain and HarvestGain:IsA("TextLabel") then
HarvestGain.Text = item_info.Harvest_Gain .. "x"
end
if HarvestSpeed and HarvestSpeed:IsA("TextLabel") then
HarvestSpeed.Text = item_info.Harvest_Speed .. "x"
end
elseif item_info.Type == "pet" then
HarvestGain_stat.Visible = true
FarmIncome_stat.Visible = true
local nameLabel = newItem:FindFirstChild("Frame"):FindFirstChild("icons"):FindFirstChild("Name")
local HarvestGain = HarvestGain_stat:FindFirstChild("Stat1")
local FarmIncome = FarmIncome_stat:FindFirstChild("Stat2")
if nameLabel and nameLabel:IsA("TextLabel") then
nameLabel.Text = item_info.Name
end
if HarvestGain and HarvestGain:IsA("TextLabel") then
HarvestGain.Text = item_info.Harvest_Gain .. "x"
end
if FarmIncome and FarmIncome:IsA("TextLabel") then
FarmIncome.Text = item_info.Farm_Income .. "x"
end
elseif item_info.Type == "pot" then
HarvestGain_stat.Visible = true
FarmIncome_stat.Visible = true
local nameLabel = newItem:FindFirstChild("Frame"):FindFirstChild("icons"):FindFirstChild("Name")
local HarvestGain = HarvestGain_stat:FindFirstChild("Stat1")
local FarmIncome = HarvestGain_stat:FindFirstChild("Stat2")
if nameLabel and nameLabel:IsA("TextLabel") then
nameLabel.Text = item_info.Name
end
if HarvestGain and HarvestGain:IsA("TextLabel") then
HarvestGain.Text = item_info.Harvest_Gain .. "x"
end
if FarmIncome and FarmIncome:IsA("TextLabel") then
FarmIncome.Text = item_info.Harvest_Speed .. "x"
end
end
-- Rarity'e göre arka plan rengini ayarla
local itemFrame = newItem:FindFirstChild("Frame")
local imageFrame = itemFrame:FindFirstChild("icons"):FindFirstChild("ItemPic")
--local Popupmenu_pic = inventoryMenu:FindFirstChild("Popup"):FindFirstChild("icons"):FindFirstChild("ItemPic")
if itemFrame and itemFrame:IsA("ImageButton") then
local rarity = item_info.Rarity or "Common"
itemFrame.BackgroundColor3 = rarityColors[rarity] or Color3.fromRGB(169, 169, 169)
end
if imageFrame and imageFrame:IsA("ImageLabel") then
local ItemImageID = item_info.ItemImage
local imageAsset = "rbxassetid://" .. tostring(ItemImageID)
-- ImageFrame'in Image özelliğini değiştir
imageFrame.Image = imageAsset
--Popupmenu_pic.Image = imageAsset
-- Eğer item_info.Type "pet" ise büyütme işlemini uygula
if item_info.Type == "pet" then
-- Mevcut boyutları al
local currentSize = imageFrame.Size
-- Boyutları 1.5 kat artır
imageFrame.Size = UDim2.new(
currentSize.X.Scale * 1.5, currentSize.X.Offset * 1.5,
currentSize.Y.Scale * 1.5, currentSize.Y.Offset * 1.5
)
end
end
-- **EquippedItem kontrolü ve UI güncellemesi**
if equippedItemsSet[itemId] then
print("Equip bulundu")
local equippedElement = newItem:FindFirstChild("Frame"):FindFirstChild("Equipped")
local equippedElement_scythe = newItem:FindFirstChild("Frame"):FindFirstChild("EquippedScythe")
if equippedElement then
if item_info.Type == "scythe" then
equippedElement_scythe.Visible = true
else
equippedElement.Visible = true
end
if item_info.Type == "pet" then
total_pets_count = total_pets_count+1
end
totalPetsLabel.Text = tostring(total_pets_count).."/4"
end
end
end
end
Merhaba,
İstediğiniz değişikliği yapmak için fonksiyonunuzu aşağıdaki gibi güncelleyebilirsiniz:
Fonksiyon tanımına isteğe bağlı bir parametre ekleyin:
Lua'da fonksiyon parametreleri varsayılan olarak isteğe bağlıdır. Yani, fonksiyonunuzu aşağıdaki gibi güncelleyebilirsiniz:
lua
function Inventory.updateInventoryGui(player, item_type)
Bu şekilde, item_type parametresi verilmezse nil değerini alacaktır. Böylece mevcut updateInventoryGui(player) çağrılarınız çalışmaya devam eder.
Itemları işlerken item_type kontrolü ekleyin:
Itemları döngü içinde işlerken, item_type parametresinin nil olup olmadığını kontrol edebilir ve eğer verilmişse sadece eşleşen item_info.Type değerlerine sahip itemları işleyebilirsiniz.
Döngünüzü aşağıdaki gibi güncelleyebilirsiniz:
lua
for _, item in ipairs(inventoryArray) do
local itemId = item.id
local itemData = item.data
local item_id = itemData.item_type
local item_info = ItemsConfig:GetItem(item_id)
-- Eğer item_type verilmişse ve item_info.Type eşleşmiyorsa, bu itemı atla
if item_type == nil or item_info.Type == item_type then
-- Bu itemı işleyin
-- [Mevcut kodunuz burada olacak]
end
end
Bu şekilde, item_type parametresi verilmezse tüm itemları işleyecek, verilirse sadece item_info.Type değeri item_type ile eşleşen itemları işleyecektir.
Toplam item sayısını güncelleyin (isteğe bağlı):
Eğer toplam item sayısını sadece filtrelenen itemlar için göstermek istiyorsanız, totalItems değişkenini de güncellemeniz gerekebilir:
lua
local totalItems = 0
for itemId, itemData in pairs(inventory) do
local item_info = ItemsConfig:GetItem(itemData.item_type)
if item_type == nil or item_info.Type == item_type then
totalItems = totalItems + 1
end
end
Bu şekilde, toplam item sayısı da filtrelenen itemları yansıtacaktır.
Güncellenmiş Fonksiyon Kodunuz:
Aşağıda güncellenmiş fonksiyon kodunuzun ilgili bölümlerini paylaşıyorum:
lua
function Inventory.updateInventoryGui(player, item_type)
-- Rarity öncelik sırası
local rarityOrder = {
Mythical = 1,
Legendary = 2,
Epic = 3,
Rare = 4,
Uncommon = 5,
Common = 6
}
-- [Mevcut kodunuz burada devam ediyor...]
-- Envanteri ve oyuncu verilerini alın
local inventory = Inventory.getInventory(player)
-- Toplam item sayısını hesaplayın (filtrelenmiş)
local totalItems = 0
for itemId, itemData in pairs(inventory) do
local item_info = ItemsConfig:GetItem(itemData.item_type)
if item_type == nil or item_info.Type == item_type then
totalItems = totalItems + 1
end
end
-- Toplam item sayısını UI'da gösterin
-- [Mevcut kodunuz...]
-- Envanterinizi bir diziye dönüştürün
local inventoryArray = {}
for itemId, itemData in pairs(inventory) do
local item_info = ItemsConfig:GetItem(itemData.item_type)
if item_type == nil or item_info.Type == item_type then
table.insert(inventoryArray, {id = itemId, data = itemData})
end
end
-- [Sıralama kodunuz...]
-- Her bir item için UI öğesi oluşturun (sıralanmış dizi kullanılarak)
local total_pets_count = 0
for _, item in ipairs(inventoryArray) do
local itemId = item.id
local itemData = item.data
local item_id = itemData.item_type
local item_info = ItemsConfig:GetItem(item_id)
-- [Item işleme ve UI güncelleme kodunuz...]
-- Eğer item_info.Type kontrolü gerekiyorsa, burada da ekleyebilirsiniz
-- Zaten inventoryArray içinde filtreleme yaptığımız için gerek olmayabilir
end
end
Notlar:
updateInventoryGui(player) çağrıları mevcut kodunuzda çalışmaya devam edecektir.
Eğer fonksiyonu updateInventoryGui(player, "pet") gibi çağırırsanız, sadece Type değeri "pet" olan itemlar işlenecektir.
Örnek Kullanım:
lua
-- Tüm itemları güncelle
Inventory.updateInventoryGui(player)
-- Sadece 'pet' türündeki itemları güncelle
Inventory.updateInventoryGui(player, "pet")
-- Sadece 'scythe' türündeki itemları güncelle
Inventory.updateInventoryGui(player, "scythe")
Umarım bu çözüm istediğiniz işlevselliği sağlar. Herhangi bir sorunuz olursa veya ek yardım isterseniz lütfen bana bildirin.