[vRP] How to use functions from client-side to server-side?

Hi , i would like to get 3 or more values of variables on client-side and send the variables to server-side.

This is my example below :


---- [ CLIENT-SIDE ] ----

local value_client = ""

local value2_client = 0

local value3_client = ""

function scriptClient()

    local user_id = vRP.getUserId(source)

    if user_id then

        value_client = "value1"

        value2_client = 5

        value3_client = "value3"

    end

end

function saveClientValue()

    value_server = value_client

    value2_server = value2_client

    value3_server = value3_client

end

---- [ SERVER-SIDE ] ----

local value_server = ""

local value2_server = 0

local value3_server = ""

function scriptServer()

    saveClientValue() -- This is the local that i would like to call the function. So this is an example :)

    if value_server == "value1" then

        print("I recived the Value1!")

        print("Sent "..value2_server.." for you !")

    elseif value_server == "value2" then

        print("I recived the Value3!")

        print("Took "..value2_server.. " from you !")

    end

end

And i tried this :

On client-side


function saveClientValue()

    value_server = value_client

    value2_server = value2_client

    value3_server = value3_client

end

AddEventHandler("saveClientValue", saveClientValue)

RegisterNetEvent("saveClientValue")

On server-side


TriggerClientEvent("saveClientValue", source)

With this attempt

I didn’t receive the values and couldn’t print it.

I don’t know if my logic is correct , because i tryed to use getInterface to link both files (client,server).lua and i had only success on server-side to call from client-side.

https://docs.fivem.net/docs/scripting-manual/working-with-events/

Thanks , i read the docs but it didn’t work for me

So i did it :


---- [ CLIENT-SIDE ]----

RegisterNetEvent("saveClientValue")

AddEventHandler("saveClientValue", function()

    value_server = value_client

    value2_server = value2_client

    value3_server = value3_client

    return value_server,value2_server,value3_server

end)


---- [ SERVER-SIDE ]----

function scriptServer()

    TriggerClientEvent("saveClientValue", source, value_server, value2_server, value3_server )

    print("The value_server is :"..value_server.." and the value2_server is :"..value2_server.." and the value3_server is :"..value3_server)

    if value_server == "value1" then

        print("I recived the Value1!")

        print("Sent "..value2_server.." for you !")

    elseif value_server == "value2" then

        print("I recived the Value3!")

        print("Took "..value2_server.. " from you !")

    end

end

On console

The values of variables don’t return for server-side.

Does anyone know how to do this?

You can return variables like this for example:

-- CLIENT 
RegisterNetEvent('RecieveVariables')
AddEventHandler('RecieveVariables', function(variable1, variable2, variable3)
       print(variable1, variable2, variable3)
end)

TriggerServerEvent('SendVariables')

-- SERVER

local variable1 = "Hello" 
local variable2 = "Hello3"
local variable3 = "Hello4"
RegisterNetEvent('SendVariables')
AddEventHandler('SendVariables', function() 
       TriggerClientEvent('RecieveVariables', source,  variable1, variable2, variable3)
end)

Thank you , it helped me a lot and i understand now , thank you so much. :relaxed:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.