Jump to content

Can how X function be shared into all resources.


Recommended Posts

It's so easy and simple, for example I created

function givePlayersMoney(value)

 blah

end

 

 function in accounts system, I don't want to make an export for it. I want it to be shared into all resources like

 

givePlayersMoney(5000) without needing an export (exports.AccountsSystem:givePlayersMoney(5000))

Link to comment

meta.xml 

<meta>
<script src="server.lua" type="server" />
<script src="client.lua" type="client" />

<export function="giveMoney" type="server" />
<export function="giveMoney" type="client" />
</meta>

 

client.lua

--[[ CLIENT SIDE ]]
function giveMoney(value)
    triggerServerEvent("source >> giveMoney", getLocalPlayer(), value)
end

 

server.lua

--[[ SERVER SIDE ]]
addEvent("source >> giveMoney",true)
addEventHandler("source >> giveMoney", getRootElement(), function(value)
    givePlayerMoney(source, value)
    return true
end)

function giveMoney(element, value)
    if isElement(element) and tonumber(value) then
        givePlayerMoney(element, value)
        return true
    else
        return false
    end
end

 

Edited by TheMOG
Link to comment
  • Moderators
8 minutes ago, Leo Messi said:

I won't an exports, and all of the functions that I gave was an example... I want the function to be shared into all resources without needing an export.. got it?

You can't do that.

Link to comment
  • Moderators

You might attach a function as string on to the root and load per resource the string?

 

Resource 1.

setElementData(root, "allFunctions", "function newFunction() end", false)
triggerEvent("allFunctions", root)

 

Resource 2, 3, 4, 5, etc.

addEvent("allFunctions", false)

local function loadAllFunctions ()
	local allFunctions = getElementData(root, "allFunctions")
	if allFunctions then
		loadstring(allFunctions)()
		removeEventHandler("allFunctions", root, loadAllFunctions)
	end
end

addEventHandler("onResourceStart", resourceRoot, 
function ()
	addEventHandler("allFunctions", root, loadAllFunctions)
	loadAllFunctions()
end)

 

 

I do not recommend it muhahahah but maybe it works for you lol. :P:P:P

really bad practice

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...