Jump to content

use a function from another script


fairyoggy

Recommended Posts

Hello, how can I use a function from one script in another?

For example, In one script, this function is written:

function changeWalkSettings (arg)
    toggleControl(source, "sprint", arg)
    toggleControl(source, "jump", arg)
    toggleControl(source, "aim_weapon", arg)
    toggleControl(source, "next_weapon", arg)
    toggleControl(source, "previous_weapon", arg)
    toggleControl(source, "fire", arg)
    toggleControl(source, "enter_exit", arg)
end 

In another script, I want to call this command with a command like:

changeWalkSettings(true) or changeWalkSettings(false)

How can I do it right?

Link to comment
function changeWalkSettings (arg)
    toggleControl(source, "sprint", arg)
    toggleControl(source, "jump", arg)
    toggleControl(source, "aim_weapon", arg)
    toggleControl(source, "next_weapon", arg)
    toggleControl(source, "previous_weapon", arg)
    toggleControl(source, "fire", arg)
    toggleControl(source, "enter_exit", arg)
end
addEvent("changeWalkSettings", true)
addEventHandler("changeWalkSettings", root, changeWalkSettings)



--// In another script it will be like this
triggerClientEvent(source, "changeWalkSettings", source, true or false)  --// for client to server
triggerServerEvent("changeWalkSettings", localPlayer, true or false) --// for server to client
triggerEvent("changeWalkSettings", localPlayer, true or false)  --// if you trigger the client function in another client script
triggerEvent("changeWalkSettings", source, true or false)  --// if you trigger the server function in another server script

or via exports

exports.theResourceWhereTheFunctionIs:theFunction(arguments)
Example: exports.myScript:changeWalkSettings(true) 

If you want to use the export method you need to put in meta.xml this:
  <export function="changeWalkSettings" type="(client, server or shared)" />
    ( type="client" will mean the function can be exported only in client scripts )
    ( type="server" will mean the function can be exported only in server scripts )
    ( type="shared" will mean the function can be exported in client and server scripts )
          
In my opinion, I recommend using the trigger method

I hope I helped you

  • Like 1
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...