Jump to content

Making a 'global' function server-sided


'LinKin

Recommended Posts

Hello,

I want to make a function server-side, so that it can be used by any resource I give access to without having to copy the function's implementation inside each resource. How can I do that?

Also,

When I do triggerServerEvent, is the event heard in other scripts? Or just in the script where the event was fired.

Thanks.

Link to comment

You have to export it inside the original resources' meta file, then use the exports function (call) to use it from other resources. As far as I know there is no way to actually hard-code it so you can just use it like the built in MTA functions though.

Link to comment

Thank you.

Now "When I do triggerServerEvent, is the event heard in other scripts? Or just in the script where the event was fired."

I ask this because I'm having a problem (like a small bug) with a script.

Server-side

SERVER.png

Client-side

CLIENT.png

* Lua tag wasn't working. And using code tag made it hard to read.

The problem with that script is that sometimes, when the players are ready and are playing, the text is not removed from their screen.

Link to comment
Thank you.

Now "When I do triggerServerEvent, is the event heard in other scripts? Or just in the script where the event was fired."

I ask this because I'm having a problem (like a small bug) with a script.

Server-side

SERVER.png

Client-side

CLIENT.png

* Lua tag wasn't working. And using code tag made it hard to read.

The problem with that script is that sometimes, when the players are ready and are playing, the text is not removed from their screen.

TriggerEvent functions works inside a resource. You can't trigger events in other resources so for this purpose we use exports or this call function.

Link to comment
Hmm.. So what do you think the reason of this 'bug' is?

This is not a bug. This is how lua works. When you download lua program for windows. We make resource in its root folder just like in mta and the main purpose for a resource is that the data doesn't mess up together.

Link to comment

The problem with that script is that sometimes, when the players are ready and are playing, the text is not removed from their screen.

That's what I'm talking about. Is like if the client didn't fire the event.. I'm not sure about what's happening there

Link to comment

You are overriding waitDisplay, waitText1, waitText2, waitText3 and waitText4 everytime, so if a player joins and another player joins before the previous player has finished downloading, then the player that joined previously wont have his text destroyed. A table should've been used, but in this case, there is no need to create new text items and display for each player.

local waitDisplay = textCreateDisplay() 
local waitText = {} 
waitText[1] = --textCreateTextItem(blablabla) 
waitText[2] = --textCreateTextItem(blablabla) 
waitText[3] = --textCreateTextItem(blablabla) 
waitText[4] = --textCreateTextItem(blablabla) 
for k, v in ipairs(waitText) do 
    textDisplayAddText(waitDisplay, v) 
end 
  
addEventHandler("onPlayerJoin", root, 
function() 
    showChat(source, false) 
    textDisplayAddObserver(waitDisplay, source) 
end) 
  
addEvent("onClientReady", true) 
addEventHandler("onClientReady", root, 
function() 
    textDisplayRemoveObserver(waitDisplay, source) 
end) 

Also,

Use outputDebugString to see the flow of your script, so you can see if an event was really triggered or not.

Link to comment

TriggerEvent functions works inside a resource. You can't trigger events in other resources so for this purpose we use exports or this call function.

Actually you can :o

Really? I've never even tested it but if thats true then why do we even use exports? :shock:

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