[Help] I can't manage to get server side scripting to work

I want to do a script for my server where I change the door opening animation for some cars:

local veh = nil
local currentVeh = nil
local VehicleLists= {my cars}

function SupportedCars(v)
    for _, model in ipairs(VehicleLists) do
        if GetEntityModel(v) == GetHashKey(model) then
            return true
        end
    end
    return false
end

function OnTick()
    local character = GetPlayerPed(-1)
    currentVeh = GetVehiclePedIsTryingToEnter(character)

    if currentVeh ~= 0 then
        veh = currentVeh
    end
    
    if GetVehiclePedIsUsing(character) ~= 0 then
        veh = GetVehiclePedIsUsing(character)
    end

    if SupportedCars(veh) then
        local angle1 = GetVehicleDoorAngleRatio(veh, 1) 

        SetVehicleDoorControl(veh, 2, 1, angle1)
    end
end

Citizen.CreateThread(function()
    while true do
        Wait(0)
        OnTick()
    end
end)

I want to change the door opening from some cars in my list. The problem is that I can’t manage to get server side synchronization to work.

What I want to achieve is, for example: player1 sees how player2 goes into his car with the new opening door animation. But no matter what I try, the door animation isn’t synced and also it looks like the default GTA door opening. Client side works without a problem.

I tried this for server.lua:
RegisterServerEvent(‘syncDoors’)
AddEventHandler(‘syncDoors’, function(vehNetId, angle)
TriggerClientEvent(‘syncDoors’, -1, vehNetId, angle)
end)

And added this to client.lua:

local vehNetId = NetworkGetNetworkIdFromEntity(veh)
        TriggerServerEvent('syncDoors', vehNetId, angle1)

and this for registering the event:

RegisterNetEvent('syncDoors')
AddEventHandler('syncDoors', function(vehNetId, angle)
    local vehicle = NetworkGetEntityFromNetworkId(vehNetId)
    if DoesEntityExist(vehicle) and SupportedCars(vehicle) then
        SetVehicleDoorControl(vehicle, 2, 1, angle)
    end

But i can’t seem to make it work. Do you have any idea on how to do this? Thanks!

So you want to open two doors at the same time? Is that what I get from your code?

Then you can forget doing anything on server side here. Always remember that there is a delay between clients and server. The client opening the first door will always be faster than you trying to do an “extra sync” for the second door.

You could probably get this to work if you are only working on client side with the player that currently has the network control over the vehicle.
Also SetVehicleDoorControl only exists on client side according to the native reference.

I don’t want to open 2 doors at once. I just want to change the door opening animation, when the player opens the door. And it works, opening doors for my modded cars look like I want to, but it isn’t synced. Synchronisation between players doesn’t work. I have no idea how to code server side functions.

To summarise it, the door opening works if you open the door of your own car, but if you watch somebody else in the server open the door of his own car, the animation looks completely different (not even default door animation, it opens without any animation).
Im wondering if it’s because SetVehicleDoor doesn’t exist in server natives, but I think there still must be a way to make this work.

Then a video would probably help in understanding what you want to achieve :slight_smile:

In your code you are doing this:

local angle1 = GetVehicleDoorAngleRatio(veh, 1) 
SetVehicleDoorControl(veh, 2, 1, angle1)

… taking the angle of doorIndex 1 and applying it to doorIndex 2. So essentially if you open door 1, door 2 opens at the same angle.

Ah yes, I wrote this on my laptop, I have my fivem server and games related stuff on my pc, and I tipped a similar code to my original code. I wrote random numbers in there.
The problem is that I don’t know how to code server side functions. I heard I have to call the NetworkId for the vehicles, but I‘m not sure how to.

OP request