I can't export anything from mysql in function

this i my function

function perm(player)
    MySQL.Async.fetchScalar('SELECT perm FROM users WHERE steamname = @steamname', {
        ['@steamname'] = GetPlayerName(player)
    }, function(result)
        permission = result
    end)
    return permission
end

this is my cmd

RegisterCommand('admint', function(source, args)
    local player = source
    print(perm(player))
end)

when i use cmd print nil why dont print my perm from database
but when i print in function in mysql print me my perm

You cannot return a function like that.

You are using the async function from mysql-async. Two examples how this could work:
Wait for async result:

function perm(player)
    local permission = nil
    MySQL.Async.fetchScalar('SELECT perm FROM users WHERE steamname = @steamname', {
        ['@steamname'] = GetPlayerName(player)
    }, function(result)
        permission = result
    end)

    while (permission == nil) do
        Wait(0)
    end

    return permission
end

Use sync method:

function perm(player)
    local permission = MySQL.Sync.fetchScalar('SELECT perm FROM users WHERE steamname = @steamname', {
        ['@steamname'] = GetPlayerName(player)
    })
    return permission
end

A return always returns the function it itself is in.

ok
but again print nil
i use

function perm(player)
    local permission = MySQL.Sync.fetchScalar('SELECT perm FROM users WHERE steamname = @steamname', {
        ['@steamname'] = GetPlayerName(player)
    })
    return permission
end

but not work

wait
for first try print nil
but after print my perm
thanks bro

i have one question
how can i do to use this for any script in out of this folder

Please rephrase that sentence, I cannot follow you :sweat_smile:

i want to use perm in out of this file
how can do this for use perm() function out of this folder

for use out of folder

You add a exports("perm", perm) after the function.

You can then call it using local perm = exports["RESOURCE_NAME_WITH_THE_EXPORT"]:perm() in any other script

Can I call it inside fxmanifest.lua and then use it?

You can find more information about exports here: Scripting in Lua - Cfx.re Docs

You can define the export inside the fxmanifest, though I usually prefer them directly inside code. You cannot use/call an export inside the fxmanifest.

thank you

1 Like