Jump to content

Passing information from server to client advice

Recommended Posts

Hello, everyone! So i've been doing some scripts in my free time. So i am doing some testing with some ideas and currently i am writing a vehicle pickup system (some kind of vehicle deathmatch). I wanted to create custom pickup elements with few additional variables and different animation. Since models cannot be attached to custom elements, i created new object and set it as a child. I did this all server-side and passed it to client-side file because i wanted it animated client-side using onClientRender.
Here is server-side file:

pickups = {}

local BOX_MDL = 1271
local respawnTime = 30000

function createVehiclePickup(pickupType, x, y, z)

    local pickup = {

        element = nil,
        model = nil,
        isActive = true,
        rot_angle = 0,
        startZ = z


    if (pickupType == 1) then
        pickup.element = createElement("vdm_pickup", "weapon_pickup")
        pickup.model = createObject(BOX_MDL, x, y, z)
        setElementParent(pickup.model, pickup.element)
        setElementCollisionsEnabled(pickup.model, false)
    elseif (pickupType == 2) then
        pickup.element = createElement("vdm_pickup", "powerup_pickup")

    table.insert(pickups, pickup)


function getPickupList()
    return pickups

addEventHandler("onPlayerChat", getRootElement(), function(message)

    if (message == "test") then
        table.remove(pickups, 1)
    elseif (message == "test2") then
        setElementModel(pickups[1].model, 1272)


And here is client-side file:

cl_pickups = {}

function clientUpdatePickupListHandler(list)

    local len = #list
    table.insert(cl_pickups, list[len])


addEvent("clientUpdatePickupList", true)
addEventHandler("clientUpdatePickupList", getResourceRootElement(), clientUpdatePickupListHandler)

local delta_phi = 6
addEventHandler("onClientRender", getRootElement(), function()

    if (#cl_pickups == 0) then return false
        for i = 1, #cl_pickups do
            if (cl_pickups[i].isActive) then
                if (cl_pickups[i].rot_angle == 360) then
                    cl_pickups[i].rot_angle = 0
                local x, y, z = getElementPosition(cl_pickups[i].model)
                local rx, ry, rz = getElementRotation(cl_pickups[i].model)
                setElementPosition(cl_pickups[i].model, x, y, cl_pickups[i].startZ + 0.2 * math.sin(math.rad(cl_pickups[i].rot_angle)))
                setElementRotation(cl_pickups[i].model, rx, 5 * math.sin(math.rad(cl_pickups[i].rot_angle)), cl_pickups[i].rot_angle)

                cl_pickups[i].rot_angle = cl_pickups[i].rot_angle + delta_phi


addEventHandler("onClientCharacter", getRootElement(), function(char)

    if (char == 'o') then
        setElementModel(cl_pickups[1].model, 1270)


Tested it and works as it should. I have few questions:

1. I am using tables to pass data. I know i can also use setElementData. Which method is better in this case?

2. I already mentioned it is animated using onClientRender. Is this good approach? I know i could use timers on server and animate it that way and i understand animations would be synced for all clients but animation will lag as the server lags. Client-side approach is independent on server connection, but it is not synced between clients. However, i think that it does not matter since animation is purely aesthetic.

3. I added onPlayerChat and onClientCharacter as a debug functions. When i type "test2", since it is server-side function, model is changed for all clients and it does. When i press 'o', since it is client-side function, it changes model for that particular player only. So if i understand right, am i passing reference to elements and objects when i pass tables from server to clients?

Thanks everyone and i apologise for a longer post!

Link to post

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