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

Table has random nil values inside of it?

Asked by 8 years ago

This is less of a big deal, and more of an annoyance. I'm assuming it's how I add the values into the table.

Let me try to walk you through my thinking process.

I have a folder full of checkpoints. When the player walks on these checkpoints, one of the players leaderstats, "Stage", gets updated to the number of the checkpoint.

Anyways, when the player dies I check the value of the players Stage leaderstat to the number index of a table. Here's what it looks like,

local wrk = game:GetService("Workspace")
local CheckpointsFolder = wrk:WaitForChild("Checkpoints")
local CheckpointsFolderChildren = CheckpointsFolder:GetChildren()
local Checkpoints = {}

for i,v in pairs(CheckpointsFolderChildren) do--Sorts the table
    local length = tonumber(string.len(v.Name))
    local num = string.sub(v.Name, 6, length)
    if length and num and v then
        table.insert(Checkpoints, num, v)
    end
end
Yes, I know this isn't the best script ever, and there's lots of not needed stuff, but this extra stuff shouldn't be the problem, so don't worry about it unless you think it's causing my problem.

The folder looks like this,

link

All the code does is put all of these in a table in order.

There are 20 spawns but when I do this to check the values of the table, I get this,

for i = 1,#Checkpoints do
    print(Checkpoints[i])
end

OUTPUT,

Spawn1
Spawn2
nil
Spawn3
Spawn4
Spawn5
Spawn6
Spawn7
Spawn8
Spawn9
Spawn10
Spawn11
Spawn12
Spawn13
Spawn14
Spawn15
Spawn16
Spawn17
Spawn18
Spawn19
Spawn21
Spawn20

As you can see, I get a nil value in the third spot of the table. I've done things like this in the past and the same thing always seems to happen

My question,

How should I be doing this? How should I insert the values, In order, into my table?

Here's what I've been doing to fix the problem,

for i = 1,#Checkpoints do
    if not Checkpoints[i] then
        table.remove(Checkpoints, i)
    end
end

And this does fix the problem, and I'll be fine if I have to do this, but I was just confused as to why this is happening.

Thank you.

1 answer

Log in to vote
1
Answered by 8 years ago

So I have been testing and this is my results:-

local a = {}

table.insert(a, 1,"1")
table.insert(a, 2,"2")

table.insert(a, 10,"10")
table.insert(a, 22,"22")
table.insert(a, 23,"23")
table.insert(a, 24,"24")

print("Number of items is " .. #a)

for i = 1,#a do
    print(a[i])
end
print()

-- output:-
--Number of items is 2
--1
--2

local itmCount = 0
for i,v in pairs(a) do
    print(i,v)
    itmCount = itmCount + 1
end
print("Number of items is " .. itmCount)
print()
-- output:-
--1 1
--2 2
--24 24
--22 22
--23 23
--10 10
--Number of items is 6

-- the insert problem
local b = {}
table.insert(b, 1,"1")
table.insert(b, 5,"5")
table.insert(b, 4,"4")
table.insert(b, 3,"3")
table.insert(b, 2,"2")
print(unpack(b))
print("Number of items is " .. #b) -- but i only added 5?
-- output:-
--1 2 nil 3 4 5
--Number of items is 6

From this there are two problems nils in arrays "Frequently, in Lua, we assume that an array ends just before its first nil element"

So where did the nil's come from? Using table.insert "Inserts element value at position pos in table, shifting up other elements to open space"

It seems that the method includes some kind of padding when adding values at different positions, but I don't know why :/

The fix In short either don't rely on the "#" operator or the table.insert method

local wrk = game:GetService("Workspace")
local CheckpointsFolder = wrk:WaitForChild("Checkpoints")
local CheckpointsFolderChildren = CheckpointsFolder:GetChildren()
local Checkpoints = {}

for i,v in pairs(CheckpointsFolderChildren) do--Sorts the table
    local num = tonumber(string.sub(v.Name, 6, tonumber(string.len(v.Name))))
    if num and v then
        Checkpoints[num] = v -- use our own index instead of using the insert method
    end
end

Output:- Spawn1 Spawn2 Spawn3 Spawn4 Spawn5 Spawn6 Spawn7 Spawn8 Spawn9 Spawn10 Spawn11 Spawn12 Spawn13 Spawn14 Spawn15 Spawn16 Spawn17 Spawn18 Spawn19 Spawn20 Spawn21

Hope this helps.

0
Wow. Thanks a ton! User#11440 120 — 8y
Ad

Answer this question