Jump to content

Synchronized vehicle components


ivan8065

Recommended Posts

21 minutes ago, ivan8065 said:

Hello, I made a script for sliding doors on custom van model through setVehicleComponentPosition but it is only client-side, would be possible to synchronize that with other players please?

Hello ivan8065,

yes we can help you add server-synchronization for your sliding doors script. Could you please post your client-side script so that we can work on it? :) 

Link to comment
addCommandHandler("dvere", function()
    local vehicle = getPedOccupiedVehicle(localPlayer)
    if not vehicle then return outputChatBox("ERROR: Musíš sedieť vo vozidle!") end
    	setVehicleComponentPosition(vehicle, "bocne_dvere", 0.1, -1.275, 0)
end)

addCommandHandler("dvereclose", function()
    local vehicle = getPedOccupiedVehicle(localPlayer)
    if not vehicle then return outputChatBox("ERROR: Musíš sedieť vo vozidle!") end
    	setVehicleComponentPosition(vehicle, "bocne_dvere", 0, 0, 0)
end)

 

Link to comment

client-side

local function open_van_door()
  setVehicleComponentPosition(source, "bocne_dvere", 0.1, -1.275, 0)
end
addEvent("onClientVanDoorOpen", true);
addEventHandler("onClientVanDoorOpen", root, open_van_door, false);

local function close_van_door()
  setVehicleComponentPosition(source, "bocne_dvere", 0, 0, 0)
end
addEvent("onClientVanDoorClose", true);
addEventHandler("onClientVanDoorClose", root, close_van_door, false);

addCommandHandler("dvere", function()
    local vehicle = getPedOccupiedVehicle(localPlayer)
    if not vehicle then return outputChatBox("ERROR: Musíš sedieť vo vozidle!") end
   	triggerServerEvent("onRequestVanDoorOpen", vehicle);
end)

addCommandHandler("dvereclose", function()
    local vehicle = getPedOccupiedVehicle(localPlayer)
    if not vehicle then return outputChatBox("ERROR: Musíš sedieť vo vozidle!") end
   	triggerServerEvent("onRequestVarDoorClose", vehicle);
end)

triggerServerEvent("onPlayerReady", resourceRoot);

server-side

local vans = {};

local function get_van_info(veh)
  local existing = vans[veh];
  
  if (existing) then return existing; end;
  
  local van = {};
  van.vehicle = veh;
  van.is_door_closed = true;
  return van;
end

addEvent("onRequestVanDoorOpen", true);
addEventHandler("onRequestVanDoorOpen", root, function()
    local info = get_van_info(source);
    
    if (info.is_door_closed) then
      triggerClientEvent("onClientVanDoorOpen", source);
      
      info.is_door_closed = false;
    end
  end
);

addEvent("onRequestVanDoorClose", true);
addEventHandler("onRequestVanDoorClose", root, function()
    local info = get_van_info(source);
    
    if not (info.is_door_closed) then
      triggerClientEvent("onClientVanDoorClose", source);
      
      info.is_door_closed = true;
    end
  end
);

addEvent("onPlayerReady", true);
addEventHandler("onPlayerReady", resourceRoot, function()
   	-- TODO: open the van doors for the client on all vans that we know about, using a for-loop and triggerClientEvent(client, "onClientVanDoorOpen", ...)
  end, false
);

Implementing the handler for "onPlayerReady" is left for you as an exercise. :) It is important so that new players that join also see open van doors.

Edited by The_GTA
  • Thanks 1
Link to comment

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