table.find, search function

As a Roblox developer, it is currently too hard to search for a specific item in an array via table.find.

Currently, you’re required to make your own function for searching since table.find only checks if the needle argument you provided is equal to the table value, creating excess functions like this can be an annoyance.

My solution to this is allowing developers to provide a function for an argument, this function would get the table value as the parameter, you can then return true or false if it’s the item you’re searching for.

A usecase for this might be the current dilemma I have at the moment, I’m looking through an array of Players for a Player with a specific UserId therefore a function is required here.

If Roblox is able to address this issue, it would improve my development experience because I would be able to use table.find instead of creating my own function that iterates through the array which’ll streamline my developing.

1 Like

You can maybe use Cryo instead, although adding a function argument would be more performant.

1 Like

I don’t get what are you asking? table.find is meant to be like string.find in the sense that it returns where the provided value is. Otherwise return nil. Why would it return true or false?

1 Like

To specify that the current Value is the Value you’re looking for.

Alright so I was able to get clarification. What was wanted was something like javascript predicates.

local Array = {workspace.Camera, workspace.Baseplate}

local FoundPosition = table.find(Array, nil, nil, function(Value)
        return Value.Name == "Baseplate"
end)

print(FoundPosition) --// 2

Though this is as easy as

local function find(t, pred)
    for pos, value in ipairs(t) do
        if pred(value) then
            return pos
        end
    end
    return nil
end

I dont believe the goal of roblox is to make Lua into javascript, the use case seems to be very niche

Yes, it’s a simple addition however it’s much needed.

Having native support rather than writing the function in your script everytime/using a ModuleScript would be much simpler.

table.find itself is just a simple search function which would be shorter than 6 lines.

The purpose of table.find is to reduce boilerplate and to improve performance when searching through large arrays. It’s important that it stays simple, lightweight, and intuitive. I get that a function argument seems flexible, but the additional function calls completely negates the performance boost, and would simply become a hacky/slow way to loop through arrays (where return true is equivalent to break, which is just bad design.) Using ipairs to test every value inline, setting the index to a variable when found, then breaking is likely much more readable for your case anyways.

What table.find is missing for me as a developer, is the ability to search starting from the end of an array instead of the beginning. Removing values near the end of an array is more performant than removing values near the beginning because it needs to shift succeeding values to close the hole. Because of this, it makes sense to design scripts such that values near the end are more “volatile” and likely to change or be removed.
Reverse ipairs would also be useful, but a reason not to add reverse methods is that array lengths needs to be resolved (which isn’t always trivial), while table.find and ipairs can simply stop at the first nil/hole in the array which is fast and simple.
Range and step parameters would complement table.find in my opinion, but I believe Roblox should be stay hesitant to add any new API without first analyzing the use cases and possible alternatives.

3 Likes