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

Why is the second element of my array just nil?

Asked by 6 years ago
Edited 6 years ago

So I'm working with datastore and saving an array called colourtable, it is supposed to store a list of integers that represent the character parts' colours. I take the colour of the part, compare it to my preset colours and then save that integer in the array based on what the name of the part is. In the loading script I colour the part again with the same index at which I saved that parts' colour. All of this has been working perfectly so far, however, the 'Head' randomly does not save properly and when I printed colourtable, expecting to see a list of 1s, the second index was just nil. There is nothing different about the head from any of the other parts and the script is entirely identical.

I wonder if there is some information about Arrays that I am just missing, why is the second element just always nil? Trouble shooting hasn't gotten me anywhere, could someone explain why this is happening or perhaps suggest a workaround

This is in the saving script

local loop = player.Character:GetChildren()
colourtable = {}

for i=1, #loop do
    if loop[i].ClassName == 'MeshPart' then
        local data = loop[i].Color
        if data == Color3.fromRGB(25,21,39) then
            if loop[i].Name == 'Head' then
                table.insert(colourtable,2,1)
            end
        end
    end
end

This is in the loading script

if colourtable[2] == 1 then
    player.Character.Head.Color = Color3.fromRGB(25,21,39)

Here I'm trying to put the integer 1 into the second element of the array if the objects name is Head and if the colour matches that preset color3. This script works for every other part except head and FR_LegTop, neither of which have anything different to any other part in script or in their properties.

0
change the first line to 'if colourtable[1] == 1 then' awesomeipod 607 — 6y
0
The index of head is [2], another part is being stored in [1]. I'm checking if it equals 1 because thats the representative number for the color3 (25,21,39) Samstergizmo 28 — 6y
0
so I want it in the second element, which is for some reason, nil when every other element I've inserted this way has retained the value 1 Samstergizmo 28 — 6y
0
if it's nil then there is something wrong when you insert the second elements. awesomeipod 607 — 6y
View all comments (9 more)
0
double check to make sure: for i = 1, #colourtable do print(colourtable[i]) end awesomeipod 607 — 6y
0
check if that returns a second elements awesomeipod 607 — 6y
0
I've printed that already, it prints 1 for every element except 2 and some random element somewhere halfway down the list. Samstergizmo 28 — 6y
0
and the insert line I use is exactly the same for all of them, I just loop through them and use the same code Samstergizmo 28 — 6y
0
try printing 'test' below 'table.insert(colourtable,2,1)'. If it doesn't print, your if statements are not working awesomeipod 607 — 6y
0
also done that one ^^; Samstergizmo 28 — 6y
0
so did it print or not awesomeipod 607 — 6y
0
Why are there three parameters in table.insert? It only requires two parameters, table.insert(TABLENAME, instance) hellmatic 1523 — 6y
0
@sickings it exists. the second parameter basically indicates the position/index the value should be in. look into the method carefully: http://wiki.roblox.com/index.php?title=Global_namespace/Table_manipulation#table.insert starlebVerse 685 — 6y

Answer this question