172 posts
Location
USA
Posted 07 November 2016 - 07:29 PM
Hi Guys!
I know you can use table.insert to add items to tables, but what about removing items from tables?
I am making a custom chat program and when a user leaves, i want their username to be erased from the users table
Thanks!
- House
756 posts
Posted 07 November 2016 - 07:33 PM
Have you considered table.remove?
2427 posts
Location
UK
Posted 07 November 2016 - 07:49 PM
set the value to nil
172 posts
Location
USA
Posted 07 November 2016 - 08:11 PM
Oh yeah, i found out that i can remove tables with table.remove but it is for a certain number so i did this to get around it
local test = {"Test", "Again"}
for k,v in pairs(test) do
if v == "Again" then
table.remove(test, k)
end
end
756 posts
Posted 07 November 2016 - 09:18 PM
Another way of doing it is having the table formatted like this
list = {
["House"] = true,
["Anav"] = true,
}
If you want to remove a user from the list, simply do list[user] = nil
Edited on 07 November 2016 - 08:18 PM
463 posts
Location
Star Wars
Posted 07 November 2016 - 09:50 PM
Oh yeah, i found out that i can remove tables with table.remove but it is for a certain number so i did this to get around it
local test = {"Test", "Again"}
for k,v in pairs(test) do
if v == "Again" then
table.remove(test, k)
end
end
Note that for i = 1, #list is ordered and faster instead of pairs. If you don't want to get the value with local object = list
then you can use ipairs.