Add a built-in table function to remove an element from an array by value

As a Roblox developer it is currently hard (or inconvenient) to remove a specific element from a table by value. There is table.remove, which removes an element from an array by key, but there is no built-in way to remove an element by value without needing to add this helper function to the game yourself:

local function remove(array, value)
    for i, v in ipairs(array) do
        if value == v then
            return table.remove(array, i)
        end
    end
end

If the issue is addressed, by adding a built-in table function, it would improve my development experience because it would mean it would be easier for me to remove a specific instance and would also mean I could remove instances with the same name and data etc, without needing to write additional code for this.

My snowball case example:

Right now I am creating a snowball small snowball game as practice, I would like to terminate the game at the end of a round (because the time has expired or because they have met the winning argument) and need to know what snowballs are around at the time. When a snowball is created it is added to a table snowballs which stores what snowballs exist at a specific time. To prevent thousands upon thousands of snowballs existing at the same time I have made it so after 30 seconds a snowball is destroyed. I am use to from other languages being able to do things like .pop({integer}) on a table to remove a specific position however lua uses .remove({integer}) with the table specified within. Now this is awkward because I’m also use to being able to do .remove({instance}) in other languages which means I can remove a specific instance but it appears lua kind of just decided it didn’t want to do that. I know other ways of removing snowballs but I personally believe that we as a platform would benefit from having a way to remove a specific instance from a list instead of having to script it ourselves or using alternate methods.

If I am searching for a specific snowball because lets say a user selects a snowball from the ground and wants to pick it up (thus its no longer needing to be despawned and isn’t in the workspace anymore) I would need to remove that specific snowball but dang all the other snowballs have the same name, properties etc just different memory locations which I as a developer may not understand. Now I need to get rid of the snowball from that table but how would I know which snowball is specifically the one I picked up without knowing the snowballs position in the table.

I’m not the best at explaining things so I’m sorry if this is all confusing and my point isn’t relayed very well :slightly_frowning_face:

1 Like

Your issue could be easily fixed by restructuring the way you handle the snowballs. You can set both the index and the value to be the snowball, meaning you can easily iterate over all the snowballs and address them when needed.

local snowballs = {}
local snowball = {} --a random snowball as an example, could be any type except for bool and nil
snowballs[snowball]=snowball --thrown
snowballs[snowball]=nil --picked up
for v in next, snowballs do --index and value are same
    --something
end

Seems like with the latest release:

You can now simplify the code to:

local index = table.find(array, element)
local poppedValue = index and table.remove(array, index)
8 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.