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

How to find data from a table inside a table?

Asked by 3 years ago
Edited 3 years ago

So first I'll need to explain the script. I'm trying to make a z-target camera system like in zelda. I have the camera system mostly done, the only thing I need now is to get a subject to target. So I made a module that gets sent(via bindable event) everything the player is touching and it puts them in a table. The item that the module gets sent is a table that holds the target item's data. This includes things like the position to move the camera its type(a value that's 1-5)ect. I want to sort the target choices based on their type. 1-5 1=boss, 2=enemy,3=NPC,4=Sign,5=other. The system will select the highest number type that is currently in range. That way you don't accidentally target an NPC when your in a fight. So the issue I'm having is sorting the tables based on type.

    local Class = {}    
    Class[1] = {}
    Class[2] = {}
    Class[3] = {}
    Class[4] = {}
    Class[5] = {}
    for index, Table in pairs(options) do
        table.insert(Class[Table.Type], Table)
    end

I have Class as a table, and each type 1,2,3,4,5 are tables inside Class. In the for loop, options is a table that hold all of the tables that are sent through the bindable event. All the tables in options are things the player can target(I.e sign, NPC ect) type is the value that classifies what each object is(I thought of using strings but numbers would mean less if statements, I could just search for the highest available number). So once i put all of the objects in Class and in side their respective tables, I run a repeat loop with a variable called count.

local count = 0
    repeat
        count = count + 1
        --I tried table.Find(Class[count], entry) and Class[Count] All of the object tables are named entry.
    until count >= 5 or Class[count] ~= nil

count will tell me what part of class[1,2,3,4,5] has entries available. The problem is I don't know how to check if a class table is nil or not. If i check Class[count] it only check if class[count] is a thing, which 1-5 are tables. How would I check if inside Class[1] table or 2,3,4,5 is nil or not?

A bit more clarification:: so I'm putting values inside of Class[1,2,3,4,5] tables, any enemy that the player can target I'm putting their value inside Class[2] table. I'm trying to read the data thats inside the class tables. if i put

if Class[1] ~= nil then
    print("Not empty")
end

it's going to print "Not empty" even if there is nothing inside Class[1]. Because Class[1] is a table. So I'm trying to see if that table has anything inside of it.

1 answer

Log in to vote
1
Answered by 3 years ago

I'm very confused on essentially what your trying too do but I'm just gonna answer you in what I think your trying too do.

So first things first if your "Class" table is going to have only 5 values at all times then instead of using pairs just make a simple for i=1,5 loop

for i=1,5 do

end

then from there check if the value is nil or not. Don't worry about order since you already have that ready/checked

for i=1,5 do
    if Class[i]~=nil then

    end
end

and finally you can continue with identify the first auto targeting the threat/other

for i=1,5 do
    if Class[i]~=nil then
        for _,target in pairs(Class[i]) do
            --Whatever you need to do here?
        end
    end
end
0
I removed the if statement and it worked, it wont run the for loop if Class[i] is empty. Thank you! Babyseal1015 56 — 3y
Ad

Answer this question