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

table.remove not working properly?

Asked by 5 years ago
Edited 5 years ago

Hello everybody, I have a problem that has been bugging me for quite a while.

So to make it easy for you to understand, let's say I have a table like so: MyMainTable = {} and a table called TheTable which is a table that comes with a function like so: .onClientEvent:Connect(function(TheTable)

So what I want to do here, is transfer the content of TheTable to MyMainTable, but to do so first I must remove the content of the table MyMainTable right? That's where the problem is at. The script simply won't remove all the content inside MyMainTable so, in the end, it gets all mixed. Here's that part of the code but with different table names:

script:WaitForChild("GiveAuditLog").onClientEvent:connect(function(TheTable)


    --//First clean the table!
    for i = 1,#AuditLog do
    local v = AuditLog[i] 
    if v then 
        table.remove(AuditLog,i)
wait(.1)
    end
    end 


-------------------------
    for i = 1,#AuditLog do
    local v = AuditLog[i] 
    if v then 
        print(AuditLog[i].." in the audit table")
wait(.1)
    end
    end

    wait(.1)
    for i = 1,#TheTable do
    local v = TheTable[i] 
    if v then 
        print(TheTable[i].." in the table received")
wait(.1)
    end
    end


--Rest of the code
end)




--More rest of code..






while wait(1) do
        for i = 1,#AuditLog do
    local v = AuditLog[i] 
    if v then 
print(v.." is here.") --Still says there are stuff remaining in the AuditLog table even after it was "supposedly" cleaned.
    end
end
end

So if the AuditLog table, which is MyMainTable were to be like AuditLog = {"1","2","3","4","5","6"} then it would print out that 2,4,6 are still here! from the while wait loop I made to test. That's the problem!

I can't use the table I received called TheTablebecause it's local to that function, and so I need to transfer it to other table to be used, but when I need to reuse that table some values will stay there mixing up the program!

I'm very very thankful to those helping.

If you need the full code, let me know.

1 answer

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

Try this code.

for i, v, in pairs(AuditLog) do
    table.remove(AuditLog, 1)
end

Basically, this'll go through each of the table values, and then delete them, so if you have three values, it'll only run three times, and -should- delete everything in it.

Also, you say your table is local to a function, I don't have your code, but couldn't you declare the table outside of it, like so,

local tablename = {}

then edit it inside of the functions separately through table.insert? That way, you can still set the values from any function, and access it from any function, removing the need for a second function.

The issue is because whenever you delete a index, all the other indexes move down one, so 2 becomes 1. So we repeatedly delete the first index for each value, effectively deleting them all.

0
I'll try it once more wilsonsilva007 373 — 5y
0
That worked! I can't believe I didn't notice that. Thank you very much! wilsonsilva007 373 — 5y
Ad

Answer this question