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

Why is #table always returning 0?

Asked by 8 years ago

I'm writing a script which rely's a ton on the number of objects in a table to properly tween them, but when a function is called which checks #mytable, the result it gets is always 0, and despite the fact that the table does have data in it (the script adds a object to the table before it runs said function, so the table must be full), it always returns 0, so if any one knows what the issue is please tell me. Here is a script which may help diagnose the issue:

local mytable = {}

function getIncerement()
    print(#mytable) --always outputs 0
    if #mytable > 1 then
        return 0.5
    else
        return 0
    end
end

function func(name)
    local obj --I would put my object here
    mytable[name] = obj
    local i = getIncerement()
    obj.Position = UDim2.new(0,0,i,0) --just pretend obj had a udim2 property
end

func("abc")
wait(1)
func("dfg")

Aaaaaaaaaand ya my issue is when the getIncerement() function is called always #mytable returns 0, if you can figure out the issue great, because this is threatening the cleanliness of a script of mine.

Thanks ~chabad360

2
You can not name a table table, or else the game will assume you're referring to the "table" functions. M39a9am3R 3210 — 8y
0
it's also important to remember that the #x syntax won't work as might be expected when dealing with dictionaries User#11893 186 — 8y
0
I know this is just for the question, but it's not like that in the actual script chabad360 34 — 8y
2
IT CERATINLY WOULD BE GREAT IF WE GOT THE SCRIPT LegitimatlyMe 519 — 8y
View all comments (3 more)
0
Well we can't exactly help you without any script.... NinjoOnline 1146 — 8y
0
I'll put a script which has good resemblance to the real one, but I don't know if it will help... chabad360 34 — 8y
0
Put the actual one, this still doesn't make complete sense! General_Scripter 425 — 8y

2 answers

Log in to vote
1
Answered by
duckwit 1404 Moderation Voter
8 years ago

Consult Section 2.5.5 of the Lua 5.1 Reference Manual:

"The length of a table t is defined to be any integer index n such thatt[n] is not nil and t[n+1] is nil; moreover, if t[1] is nil, n can be zero."

The result of the length operator (#), when applied to a table, will be correct if each integer index from 0 to n (for some value of n) is non-nil.

I propose two possible solutions:

  1. Introduce a new variable (e.g. myTableLength). Update it manually whenever objects are added or removed from the table. In your func, you would write myTableLength = myTableLength + 1.

  2. Use a generic table entry counting function to compute the length:

function tableLength(t)
    local count = 0
    for _ in pairs(t) do 
        count = count + 1 end
    return count
end
0
I thought of that but I really don't want to but I guess I have to... chabad360 34 — 8y
Ad
Log in to vote
3
Answered by 8 years ago

This is because your table is composed in dictionary format. There are two kinds of tables:

Arrays (Lists)

A table comprised of only numeric keys (i.e, just having single elements in a table)

Dictionaries (Maps)

A table comprised of manually defined keys (i.e, assigning variable-like conditions to a value inside the table)

Dictionary (or Map) example

-- "x","y","z" overwrites our numeric keys 1,2,3.

local Dictionary = {
    x = "hello",
    y = "i am",
    z = "a dictionary"
}

Array (or List) example

-- Nothing overwrites our default numeric keys, therefore they remain 1,2,3, allowing us to use the # operator on the table to return the number of elements.

local Array = {
    "hello",
    "i am",
    "an array"
}

I would go on, but honestly everything I'd explain here I've already explained in another answer that you may find very useful here:

https://scriptinghelpers.org/questions/25964/what-are-tables#29555

Answer this question