Hey I want to search the players inventory for something with “Ram” in its name but it does not work I think I got the params wrong could someone help? Code:
if string.match(Inventory, "Ram") then
Hey I want to search the players inventory for something with “Ram” in its name but it does not work I think I got the params wrong could someone help? Code:
if string.match(Inventory, "Ram") then
Could you provide more information? Such as what is the Inventory defined as.
the inventory is a folder inside the player
You would need to iterate through the children of the folder
local foundItem
for _, item in ipairs(Inventory:GetChildren()) do
if string.match(item.Name, "Ram") then
foundItem = item
break
end
end
if foundItem then
print("Found an item matching with Ram.")
end
I tired this but I think I did something wrong because I get no errors but if does not work
local player = game.Players.LocalPlayer
local Inventory = player:WaitForChild("Inventory")
local motherboardFrame = player.PlayerGui.WorkshopGui.MotherboardFrame.ScrollingFrame
local coolerFrame = player.PlayerGui.WorkshopGui.CoolerFrame.ScrollingFrame
local cpuFrame = player.PlayerGui.WorkshopGui.CpuFrame.ScrollingFrame
local gpuFrame = player.PlayerGui.WorkshopGui.GpuFrame.ScrollingFrame
local powerSupplyFrame = player.PlayerGui.WorkshopGui.PowerSupplyFrame.ScrollingFrame
local ramFrame = player.PlayerGui.WorkshopGui.RamFrame.ScrollingFrame
local storageFrame = player.PlayerGui.WorkshopGui.StorageFrame.ScrollingFrame
local template = player.PlayerGui.WorkshopGui.Template
local InventoryButton = player.PlayerGui:WaitForChild("WorkshopButtons").Inventory
InventoryButton.MouseButton1Click:Connect(function(player)
for _, item in pairs(Inventory:GetChildren()) do
local newTemplate = template:Clone()
newTemplate.Name = item.Name
newTemplate.ObjectName.Text = item.Name
newTemplate.Visible = true
local foundItem1
local foundItem2
local foundItem3
local foundItem4
local foundItem5
local foundItem6
local foundItem7
for _, item in ipairs(Inventory:GetChildren()) do
if string.match(item.Name, "Motherboard") then
foundItem1 = item
break
end
end
for _, item in ipairs(Inventory:GetChildren()) do
if string.match(item.Name, "Cooler") then
foundItem2 = item
break
end
end
for _, item in ipairs(Inventory:GetChildren()) do
if string.match(item.Name, "Cpu") then
foundItem3 = item
break
end
end
for _, item in ipairs(Inventory:GetChildren()) do
if string.match(item.Name, "Gpu") then
foundItem4 = item
break
end
end
for _, item in ipairs(Inventory:GetChildren()) do
if string.match(item.Name, "PowerSupply") then
foundItem5 = item
break
end
end
for _, item in ipairs(Inventory:GetChildren()) do
if string.match(item.Name, "Ram") then
foundItem6 = item
break
end
end
for _, item in ipairs(Inventory:GetChildren()) do
if string.match(item.Name, "Storage") then
foundItem7 = item
break
end
end
if foundItem1 then
newTemplate.Parent = motherboardFrame
elseif foundItem2 then
newTemplate.Parent = coolerFrame
elseif foundItem3 then
newTemplate.Parent = cpuFrame
elseif foundItem4 then
newTemplate.Parent = gpuFrame
elseif foundItem5 then
newTemplate.Parent = powerSupplyFrame
elseif foundItem6 then
newTemplate.Parent = ramFrame
elseif foundItem7 then
newTemplate.Parent = storageFrame
end
newTemplate.MouseButton1Click:Connect(function()
game.ReplicatedStorage.Inventory:InvokeServer(item.Name)
end)
end
end)
What’s inside the Inventory when the code runs?
Can you describe what the entire code is meant to do?
When a player clicks the inventory button it opens GUI that is supposed to display the parts in their inventory in different sections of the gui
So I rewrote your code so that instead of repeating a lot, it uses a lookup table to find the parent. If this doesn’t work try using the debugger to step through the code to see what it runs.
local ItemParenting = {
Motherboard = motherboardFrame,
Cooler = coolerFrame,
Cpu = cpuFrame,
Gpu = gpuFrame,
PowerSupply = powerSupplyFrame,
Ram = ramFrame,
Storage = storageFrame
}
InventoryButton.MouseButton1Click:Connect(function(player)
for _, item in pairs(Inventory:GetChildren()) do
local newTemplate = template:Clone()
newTemplate.Name = item.Name
newTemplate.ObjectName.Text = item.Name
newTemplate.Visible = true
for name, parent in pairs(ItemParenting) do
if string.match(item.Name, name) then
newTemplate.Parent = parent
break
end
end
newTemplate.MouseButton1Click:Connect(function()
game.ReplicatedStorage.Inventory:InvokeServer(item.Name)
end)
end
end)
Thanks you so much this worked perfectly but could you explain a little on how the for loops work I don’t really know. But you don’t have to if you don’t want to
The outer for loop loops through all of the items, and for each of them it will create one of the UI instances. The inner for loop is used for setting the parent; it uses something that’s sometimes referred to as a lookup table (an input is mapped to something else). With the lookup table, it searches through it, checking if the key/index of the lookup table matches with the item name; if it’s name matches with the key/index, then the mapped value will be used as the parent, and it will then break the inner for loop to stop it from doing unneeded passes of the code.
your the best you helped so much I will get my friend to mark your code as the solution too so don’t worry