Table.find() and tables with functions

I was wondering if table.find() works with tables that have “functions” in them. Like this.

local FunctionsForHitbox = {
	["Scythe Zombie"] = function(Player:Player?, Handle:BasePart?)
		if Player.Character:FindFirstChild("GrimScythe") then
			return Handle
		else
			return Player.Character:FindFirstChild("Right Leg")
		end
	end,
	["Baton Zombie"] = function(Player:Player?, Handle:BasePart?)
		if Player.Character:FindFirstChild("Baton") then
			return Handle
		else
			return Player.Character:FindFirstChild("Right Leg")
		end
	end,
}
local MorphsList = {
	"Scythe Zombie",
	"Baton Zombie"
}
function Check(Player:Player?, Handle)
	if not CLN:Get(Player, true) then
		return Handle
	else
		if table.find(MorphsList, CLN:Get(Player, "string")) then
			return FunctionsForHitbox[CLN:Get(Player, "string")](Player, Handle) -- This is the culprit.
		else
			return Player.Character:FindFirstChild("Right Leg")
		end
	end
end
local Hitbox = RaycastHitbox.new(Check(Player,Handle))
2 Likes

Well I have no idea what CLN is so I can’t tell you for sure if it works based on the arguments.

CLN is my game OOP handler, it returns the player zombie type if given argument is string.
However my main question is the check function and its returns.

Well in this case the table.find is only working with the MorphsList table and not FunctionsForHitbox so I don’t think there’s anything wrong here.

Maybe save the result of CLN:Get though? (If it does some resource intensive task)

1 Like

Using table.find() with Functions

table.find() is a built-in function in Lua that searches for a specific value in an array and returns its index if found. It can be used with tables that contain functions as values.

Here’s an example script that demonstrates how table.find() can be used with functions:

local myTable = {
    ["a"] = function() print("Function for key 'a'") end,
    ["b"] = function() print("Function for key 'b'") end,
    ["c"] = function() print("Function for key 'c'") end
}

local keysToSearch = {"a", "b", "c"}

for _, key in pairs(keysToSearch) do
    if table.find(keysToSearch, key) then
        myTable[key]()
    end
end

In this example, we have a table called myTable that contains three keys ( "a" , "b" , and "c" ) with corresponding functions as values. We also have an array called keysToSearch that contains the keys we want to search for in myTable .

We use a for loop to iterate over each key in keysToSearch . For each key, we use table.find() to check if the key exists in keysToSearch . If it does, we call the corresponding function from myTable using the syntax myTable[key]() . This will execute the function associated with that key.

When you run this script, you should see the following output:

Function for key 'a'
Function for key 'b'
Function for key 'c'

I hope this helps clarify how you can use table.find() with functions.

1 Like

Ah alright, is there anyways I can shorten that though? I don’t like that many returns but I couldn’t think of anything.

You would need the reference to the function, since the function is passed by reference. Try this:

local function myFunction()
    print("yay!")
end;

local _table = { myFunction };

print(table.find(_table, myFunction))

So, yes, it does work if you have a reference to the function, but if you’re trying to use it with the function’s identifier, it won’t.

You just have to understand pass-by-reference, and if the function you’re comparing is the same one that is inside the table, and not a copy created by requiring the same module.

1 Like

Maybe remove the else statements. return short-circuits anyway and it’s good practice to keep indent levels low

function Check(Player:Player?, Handle)
	if not CLN:Get(Player, true) then
		return Handle
	end
	if table.find(MorphsList, CLN:Get(Player, "string")) then
		return FunctionsForHitbox[CLN:Get(Player, "string")](Player, Handle) -- This is the culprit.
	end
	return Player.Character:FindFirstChild("Right Leg")
end

Are you using the guard clause technique?

2 Likes

What is “short-circuiting”? I’ve never heard of that phrase before.

Maybe.

Maybe it’s not the right terminology (it probably isn’t) but when you return something in one code-path, it stops all other code-paths.

For example, print("Never prints") never prints

function ex()
    if true then
        return
    end
    print("Never prints") -- Because we already returned a value, there is no need to continue execution. Thus, this never executes.
end

function ex2()
    if true then
        if false then
            -- ...
        else
            return
        end
    end
    print("Never prints")
end

But you probably already know this.

What the term short-circuiting means is more important with the or and the and operators.

For or, if the first operand is true, it never evaluates the second operand.

For and, if the first operand is false, it never evaluates the second operand.

Ex

_ = false or print("Prints!")
_ = true or print("Never prints")
_ = false and print("Never prints")

Lua/u is smart. It knows that if the first operand to and is false, it doesn’t matter what the second operand is, in either case it will result in false at the end so it takes a shortcut and returns false early. That is short-circuiting. (That’s what I know, correct me if I’m wrong)

2 Likes

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