Search code examples
functionluafivem

Disable function to enable another one


Hello sorry for disturbing i am currently working on a Anti cheat on fivem

But I have a little problem in fivem/gta you have default Natives/Functions

Exempel

IsPedInAnyVehicle(ped, boolean) -- it returns when a ped is in a vehicle

What I am trying to do is catch the function

like this

function IsPedInAnyVehicle(ped, boolean)
   -- i want to put my conditions here and when the conditions fit it accepts the real default 
   --   native/function
end

Catching the function and it is blocking the native/default function of the game but now is the question when the condition fits i want to execute the real function/native

I thinked to delete the function thad i made when the conditions fit but idk if thad is possibel Thx in advance

Neo


Solution

  • Simply overwrite the function with the new one and keep a reference to the original function so you can use it if your condition is met.

    function someFunction()
      print("I'm the old function")
    end
    
    local backup = someFunction
    someFunction = function ()
      if condition then
        backup()
      else
        print("Hey I'm the new function!")
      end
    end
    
    
    someFunction()
    condition = true
    someFunction()
    condition = false
    someFunction()
    

    prints

    Hey I'm the new function
    I'm the old function
    Hey I'm the new function!