Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to get two specific things from a table?

Asked by 4 years ago

I have a list of items in a table like

local inventory {slot1, slot2, slot3, slot4, slot5}

what i am trying to do is check if two of the items in the table has a bool value set as true

like slot1.Value = true

i am trying to figure out how to use for i,v in pairs to get them but dont now how

i need both items in the table to be set to a variable so i an exchange value between the items in the table.

2 answers

Log in to vote
0
Answered by 4 years ago

If I'm not misunderstanding you, your table should look like:

{
["Slot1"] = true
["Slot2"] = false
...
}

This should be how you use for loops in tables:

for i = 1,#inventory do
if inventory[i] == true then
-- do something
end
end

If you want do check there's 2 item's value is true:

local temp = 0
for i = 1,#inventory do
if inventory[i] == true and temp ~= 2 then
temp = temp + 1
elseif temp == 2 then
print("Found 2 items")
break
end
end

I typed it here without testing it, not sure it works or not.

Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

I don't that is quite what i mean. Let me explain more.

Basically i have an inventory system i just made and what i am trying to add to it is if i click one item in the inventory and then click another, the item in both slots will switch or if one is empty, the item will switch to the empty slot. i made a boolvalue under each slot set to false and each slot is in a table, if one slot is clicked the boolvalue is switched to true but nothing happens, only when another slot is clicked will they switch. So i am trying to check if two random slots in a table is set to true and assign each slot as a variable so i can change all the number values and images between each other.

inventory = {slot1, slot2, slot3, slot4, slot5} i know how to check if one item in a table like this

for i, v in pairs(inventory) do if v.value.Value = true then

so how do i check if two items in table is set to true? i wanna be able to do like this say if slot 1 and slot 4 are both true


slot1.value.Value = slot2.value.Value slot1.item.Value = slot2.item.Value slot1.ImageLabel.Image = slot2.Imagelabel.Image
0
For swapping variables in arrays, always create a temporary variable to save the swapped data Headstackk 45 — 4y

Answer this question