Function as variable should work like it should

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve?
    I want this function as a variable work like it does with other function

  2. What is the issue?
    The function require Humanoid to be called again when the current humanoid called the function

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    Adding the humanoid variable to the function

local Humanoid = route.to.humanoid

local GetEnabled = Humanoid.GetStateEnabled

print(GetEnabled(Enum.HumanoidStateType.Running)) -- print out Expected ':' not '.' calling member function GetStateEnabled

print(GetEnabled(Humanoid,Enum.HumanoidStateType.Running)) -- print true

Related Post:

Have you tried ModuleScripts?

local module = {}

function module.Test1()

end

function module:Test2()

end

return module
local m = require(mod)

m.Test1()
m:Test2()

(didnt read right, lol)

1 Like

Yes, this is how it’s done. What’s the issue?

You could curry the function, but then it’d only work for that specific humanoid. I.e.

function curry(f, value)
    return function(...)
        return f(value, ...)
    end
end

local GetEnabled = curry(Humanoid.GetStateEnabled, Humanoid)
1 Like

Well it should work fine with this
GetEnabled(Enum.HumanoidStateType.Running)

and also this but it has the Humanoid variable called again

GetEnabled(Humanoid,Enum.HumanoidStateType.Running)

when the GetEnabled function already called by Humanoid
as show here
local GetEnabled = Humanoid.GetStateEnabled

1 Like

All Humanoid objects share the same function. This is a good thing because it means you don’t need to have duplicate code taking up RAM everywhere.

local humanoid_1 = route.to.humanoid
local humanoid_2 = route.to.other.humanoid
print(humanoid_1.GetStateEnabled == humanoid_2.GetStateEnabled)
--> true (meaning they are literally the same function at the same location in memory)

So you’re not getting route.to.humanoid's function, you’re getting Humanoid class’s function. Because of that, you still need to specify which humanoid you’re working with.

As an aside, this also works for that reason:

game.Destroy(workspace.Part) -- destroy workspace.Part
2 Likes

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