TriggerClientEvent with values in variables

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

function scriptClient()

    if code() then

        value_client = "value1"

        value2_client = 5

        value3_client = "value3"

    end

end

RegisterNetEvent("saveClientValue")

AddEventHandler("saveClientValue", function(value_client,value2_client,value3_client)

    local value_server = value_client

    local value2_server = value2_client

    local value3_server = value3_client

end)


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

function scriptServer(source)

    TriggerClientEvent("saveClientValue", source)

    -- Print to test if value was received

    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

With this attempt

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

If you want to receive any value from client to server, the event handler should be server-side.

--[[CLIENT]]--
local value1 = 5
local value2 = "critter"
local value3 = value1 * 2

TriggerServerEvent('sendValuesToServer', value1, value2, value3)

--[[SERVER]]--

RegisterNetEvent('sendValuesToServer')
AddEventHandler('sendValuesToServer', function(_value1, _value2, _value3)
local src = source
print('I received values '.._value1..', '.._value2..' and '.._value3..'from '..GetPlayerName(src))
end)

Try this.

Long story short: with TriggerServerEvent, every param after event name is a param that can be accessed with AddEventHandler

For TriggerClientEvent, you do the same thing, but you put your params after the source value.

1 Like

Thank you very much ! It’s exactly what I needed ! You helped me a lot, thank you very much again. :blush: