Two functions with one name

Hello I would like to ask you if is possible to make two functions with same name for example function from QB-Core self.Functions.SetInventory. I need it twice so whenever I trigger Player.Functions.SetInventory… it trigger both functions one default in QB-Core and one in different script. Is it possible or is it bullshit?

why dont u just trigger both?
the self.Functions.SetInventory and the second one

I’m no expert but one name for two functions doesn’t sound good to me :wink:

Hey there!
Unfortunately, Lua does not allow the same function to be registered multiple times. I mean that you can create functions with the same name in different scripts but not in the same script. Just use TriggerEvent or exports. It will work quite well.

And quick info about functions:

function Test()
    print("works!")
end

function Test()
    print("works better!")
end

Test() -- will print "works better!"

Here the function that is registered later will overwrite previous one, but in this case:

Test = {}
OtherTest = {}

function Test:Test() -- same as Test.Test
    print('works!')
end

function OtherTest:Test()
    print('works better!')
end

Test:Test()
OtherTest:Test()

both of these will return something. I’ve added second example as to show it depends on where you put this function name.

Short answer:
For the way you described, yes it’s possible. Go for it!

Long answer:
@rocket5115 answered it well for you, but more generalized. Specific to your question, it sounds like you CAN do what you want, if I understand you correctly. If you want to create a function called SetInventory, you can do so. The QBCore one is a function property of the Player object’s “Functions” property. This means you call it by getting the player object with QBCore.Functions.GetPlayer(source) or whatever it is, then you call that function with Player.Functions.SetInventory(). Not sure what goes inside that function as parameters, but you get my point. If you make a new function in any script, it can be called SetInventory just fine. Because it’s not called Player.Functions.SetInventory. Same name but it’s not a property of the QBCore player object’s “Functions” property. This is basically the same as having the same function name in multiple scripts.

Script 1 can have function Example() and script 2 can have function Example(). Each will do it’s own thing and they don’t conflict, because they are in different location and script 1 and script 2 don’t know what functions each other has. Even if you have them as exports they will be fine, because you put what script each export comes from, such as exports[“qb-core”]:GetCoreObject().

So in that case exports["script1"]:Example() and exports["script2"]:Example() will both work fine and won’t conflict. Even in the same script with Example() on the client side and Example() on the server side is fine. Because one is client, one is server. But having 2 functions with the same name inside the same script and both client side or both server side, WILL conflict. That’s the only time it’s an issue.

So to summarize, if you are creating a function in some script called SetInventory, that is perfectly fine. Just don’t create another self.Functions.SetInventory inside the player object’s “Functions” property of QBCore, or it will override the first one.

So basically I can’t do what I need but I managed how to fix it in different way. But thanks a lot for your time. It helped me to realise that this isn’t a right way.