loltaco1110
(Amateur_Programmer)
September 17, 2023, 3:52pm
#1
I want to remove an element from a table
table.remove does not throw an error, but also does not remove the specified element
Element 5 should be gone: (but it is not)
(this is printed after the table.remove code runs)
Code:
--[[
Assume targetSlot has a value of 5 (I checked dont worry)
Data = {
["1"] = {},
["2"] = {},
["3"] = {},
["4"] = {},
["5"] = {},
["auto_save"] = {}
}
]]
-- removing data
table.remove(data, tostring(targetSlot))
print("Updated data:", data)
-- updating player data
dataStore:SetAsync(player.UserId, data)
yo9sa
(yosa)
September 17, 2023, 3:54pm
#2
This is because table.remove() only accepts a number.
Try this:
table.remove(data, table.find(data, tostring(targetSlot)))
3 Likes
loltaco1110
(Amateur_Programmer)
September 17, 2023, 3:56pm
#3
Okay this might work, going to try now
loltaco1110
(Amateur_Programmer)
September 17, 2023, 3:59pm
#4
Element 5 is still there (did not work)
Code:
-- removing data
table.remove(data, table.find(data, tostring(targetSlot)))
table.remove
only removes elements from arrays, which have numbered keys. Your keys are strings, so table.remove
will not work.
Either change your keys to numbers or set the key to nil by doing t["5"] = nil
or t[tostring(targetSlot)] = nil
.
1 Like
loltaco1110
(Amateur_Programmer)
September 17, 2023, 4:02pm
#6
But what if I want to remove the key entirely?
Have you tried it? Setting the value of a key to nil will remove it completely.
yo9sa
(yosa)
September 17, 2023, 4:04pm
#8
You wrote data with a small letter, that’s the error.
loltaco1110
(Amateur_Programmer)
September 17, 2023, 4:04pm
#9
Also I noticed that it removes the element at a index, so it should work… I think
loltaco1110
(Amateur_Programmer)
September 17, 2023, 4:05pm
#10
There is no error?
extra charactersextra charactersextra charactersextra charactersextra charactersextra charactersextra characters
yo9sa
(yosa)
September 17, 2023, 4:05pm
#11
data = {
[“1”] = {},
[“2”] = {},
[“3”] = {},
[“4”] = {},
[“5”] = {},
[“auto_save”] = {}
}
]]
– removing data
table.remove(data, table.find(data, tostring(targetSlot)))
print(“Updated data:”, data)
– updating player data
dataStore:SetAsync(player.UserId, data)
McThor2
(McThor2)
September 17, 2023, 4:06pm
#12
What is wrong with just doing data[key] = nil
?
loltaco1110
(Amateur_Programmer)
September 17, 2023, 4:06pm
#13
What about that?
extra charactersextra charactersextra charactersextra charactersextra charactersextra characters
loltaco1110
(Amateur_Programmer)
September 17, 2023, 4:06pm
#14
I want to remove the element completely, wait I was thinking of
data[key] = {} to remove the data
hold on let me test
data[key] = nil
OP could have mistyped the variable name in the post.
1 Like
McThor2
(McThor2)
September 17, 2023, 4:08pm
#16
Setting it to nil will completely remove it, that’s one of the features of Lua. Setting it to an empty table seems like a reasonable alternative too if you want to preserve the keys
loltaco1110
(Amateur_Programmer)
September 17, 2023, 4:09pm
#17
Okay yeah
data[key] = nil
worked
idk why I was thinking of
data[key] = {}
when I saw
data[key] = nil
Thanks everyone!
1 Like
system
(system)
Closed
October 1, 2023, 4:09pm
#18
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.