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

Why my script do not this remove all in the table?

Asked by 6 years ago
Edited 6 years ago

Why my script do not this remove all in the table? Somebody know how to fix my problem? I know I ask a lot questions but I am trying to learn..

--< Creates a table and insert values.
local Maps = {}
local AllMaps = RStorageService.Maps:GetChildren()

for i = 1, 3 do
    local map = AllMaps[math.random(1, #AllMaps)]
    table.insert(Maps, map)
end
print(Maps[1], Maps[2], Maps[3])

OutPut: Asylum, Mansion, Mansion

--< Now remove all values.
for i = 1, #Maps do
    table.remove(Maps, i)
end
print(Maps[1], Maps[2], Maps[3])

OutPut: Asylum, nil, nil

0
"Why my script do not this remove all in the table?" You might want to edit that, haha. T0XN 276 — 6y

1 answer

Log in to vote
0
Answered by
T0XN 276 Moderation Voter
6 years ago
Edited 6 years ago

Hi NiniBlackJackQc,

For looping through tables, use the in pairs loop and iterate by setting each value as nil. table.remove shifts the elements over to fill in the empty space, which is why all values weren't set as nil.

for i, v in pairs(Maps) do
    Maps[i] = nil
end
print(Maps[1], Maps[2], Maps[3])

Hope this answered your question!

Ad

Answer this question