Whats the best way to go between server and client

I’ve started fivem development about a week ago but I still don’t know what the best way to get information is from client in server and vice versa.
The two options I know are triggering events (but those can’t return values) and exporting functions.

Lets say I have a native client eventhandler, that gets information from the server side and then gets back to the client side to perform the action. Do I need to make a trigger to the server side and then another trigger back to the client side? Whats the best way to do this? I have literally seen zero posts about this

There are loads of examples on Callbacks, State bags, Convars.

Essentially exactly what you are describing. But it really depends on your needs. If you just need to request some data (e.g. for a client side menu) you can use callbacks. There are already several resources out there that do exactly that.

Just by searching the Release section I found this one: [RELEASE] Lua Callback system
(and my own of course :wink: ) [Release] Callbacks using exports and with added timeouts! (will also get a small update soon™)

If you constantly need some data synced between clients and the server, you should use state bags. There are several examples in the docs: State bags - Cfx.re Docs
And you need the change handler function as well: AddStateBagChangeHandler - FiveM Natives @ Cfx.re Docs
There was a small tutorial somewhere in the forum as well, but I cannot find it right now.

1 Like

Hey, Ive installed your resource and it works like a charm. However is there a way I can wait for an action to finish before continuing? This might be a whole different topic but I thought I might get the best response here.

exports["kimi_callbacks"]:Register("getSpawnCoords", function(source)

    MySQL.Async.fetchAll("SELECT * FROM player WHERE identifier = '"..GetPlayerIdentifiers(source)[4].."'", {}, function(result)

            coordx = result[1]["xpos"]
            coordy = result[1]["ypos"]
            coordz = result[1]["zpos"]
            heading = result[1]["heading"]
    
            print(print1 .. coordx)
        
        end)

        print(print2 .. coordx) 
        return coordx, coordy, coordz, heading

end)

in this example print1 gets the data right, but print2 and the return gets executed before print1 because it is still getting the data in async which causes the print2 and return values to return nill. Is there a way I can wait untill the data from the database has been collected before continuing?
Ive also tried running MySQL.Sync but that doesnt seem to do anything, the documentation also doesnt seem to mention anything about this.

Look at using promises and citizen.await

Ive looked into citizen.wait but I thought it only allowed for a given set of ms. Isn’t there a way I can wait untill completion?

Citizen.Await is different to Citizen.Wait.

Await wants a promise to be returned and waits for that return promise.

Oh sorry Ive read that wrong.
Where should I place this then? I tried placing it before the sql prompt but that doesnt work. The documentation for it is also doesnt give me any info

Citizen.Await(awaitable)

Thanks a lot :heart:

I personally haven’t worked with promises a lot, so I won’t go into those.

You might have been doing something wrong then. Instead of calling a separate function, it just returns the result inline:

exports["kimi_callbacks"]:Register("getSpawnCoords", function(source)

    local result = MySQL.Sync.fetchAll("SELECT * FROM player WHERE identifier = '"..GetPlayerIdentifiers(source)[4].."'", {})

    coordx = result[1]["xpos"]
    coordy = result[1]["ypos"]
    coordz = result[1]["zpos"]
    heading = result[1]["heading"]
    
    print(print1 .. coordx)

    return coordx, coordy, coordz, heading
end)

(Also works for oxmysql.)