Hello, i want to check to see how many of the same object are in a table. How can I do this?
Let’s say you have a table with numbers in it, and you want to see how many 5’s there are.
local myTable = {3, 1, 5, 2, 5, 8, 9, 5}
To do this, you should loop through myTable and add to a variable each time a 5 shows up.
local n = 0
local target = 5
for i,v in pairs(myTable) do
if(v == target) then
n = n + 1
end
end
At the end, n will give you the number of target objects in the table.
This an easy way of doing it, there’s probably a better way of doing it though.
local randomTable = {}
local amount = 0
local object = ""
for i,v in pairs(randomTable) do
if i == object then
amount += 1
end
end
I have a table which adds gets a item added and its a string. When i try your code the if statement doesn’t work. How can I fix this?
Table with two items in it.
Inventory = {
[1] = Sword;
[2] = Sword;
[1] = Spear;
[2] = Spear;
--etc
}
If you’re trying to get the pair rather than the key, just change that i into a v.
Hello, I fixed that issue but this still hasn’t fixed my main problem. When I pick up a different item it adds the amount from the previous items and continues to add up the total for the next item. How can I fix this?
If your doing this multiple times, just reset the amount variable in between. Also, sorry if I’m not giving the answers you need your explanations are kind of confusing.
I did that but Im still getting the error.
local function AddItem(UI, item)
local itemType = ItemModule[item.Name]["Type"]
local amt = 0
for i,v in pairs(Inventory[itemType]) do
if (item == v) then
print(i)
amt = i
end
end
for counter = 1,20 do
local slot = UI:WaitForChild("Slot"..counter)
local text = tonumber(slot.Amount.Text)
if slot.Val.Value == "" then
slot.Val.Value = item.Name
slot.Amount.Text = amt
amt = 0
break
elseif slot.Val.Value == item.Name then
slot.Amount.Text = amt
amt = 0
break
end
end
end
Okay, so exactly what is your script trying to do? Give more information on what everything is because there’s a lot of variables that I don’t know what they are.
when an item is added it calls a function and then adds the amount to a slot which is in range from 1 - 20. Im trying to fix the amount which is added but that is the issue im having.
What’s the point of getting the variable text when it’s not used in the script? And why are you setting amt = i when on this post your asking how to see how many of the same value are in a table?
that was something that I was trying out but I never did. I also found out that my table prints:
Inventory = {
[1] = Sword,
[2] = Sword,
[3] = Sword,
[4] = Sword,
[5] = Spear,
[6] = Spear,
[7] = Spear,
[8] = Spear
}
Im not sure if this changes anything but if it does what can I do to fix it?
edit: Im not sure, I want to find out how many of those items i have in the table
Okay, but I don’t understand what the problem is, like I don’t understand what your saying. Is the problem with the amt variable? Because every time the loop iterates through the table it’s going to set amt to i. If there are multiple of the same item in the table, it’s going to set i to which ever one it gets to last. I don’t see anything wrong with your script and if it’s adding it to the module script correctly it should work, but the problem is where it comes in with multiple of the same items. So first of all why is there multiple of the same items in the first place and how are you going to tell which one is the newly added one? If you need to tell which on is the newly added one, you should probably not only have the item name, but the index as well. You also wouldn’t have to loop through the table too.
Basically in short, the only way you can tell the difference of the multiple items is by the index and as it iterates through the table, it’s going to save i as which ever is the last one it gets to.
Oh now I see what your trying to do. I’ll look at your code again.
I’d suggest checking if that specific item has already filled up a slot or not. If it has, add the object you picked up to that slot of the already existing amount of items in it, if not check which slots are filled and then add it to the next one over. I don’t think you need to iterate through the table and count the number of items or get the index. It’s not relevant to what your trying to do.
I have a value under the slot name which contains the items name. Thats what the if slot.Val.Value == "" then
does.
Okay, well you should just complete remove the loop at the top and start with the second loop. If it equals the items name, then obviously you will just add one to the number and be done. If it doesn’t equal the items name then just set the amt to 1 and the name to the item name. That should fix your problem and work fine.
local function AddItem(UI, item)
for counter = 1,20 do
local slot = UI:WaitForChild("Slot"..counter)
if slot.Val.Value == "" then
slot.Val.Value = item.Name
slot.Amount.Text = 1
break
elseif slot.Val.Value == item.Name then
slot.Amount.Text = tonumber(slot.Amount.Text) + 1
break
end
end
end
Before you were setting the amount to the index of the items. You need to add the amount and the code I showed is probably the easiest way.
thanks for your help anyway, Im probably going to play around with this and try it out.
If you know what specific values there are, loop through the table and add up for the new table which include the type of values. Once you do the loop, you incrementally add 1 to each type you find in the original table. Then you finish by reading it from the new table.
Alternatively, the new table is empty and for each unique entry you find, create a new entry and add up on it.
I had an idea but im not sure how to implement it, I want to detect if there are not any item slots available, if so then change a value.