Jump to content

Help with setVehicleComponentVisible


Carlitos

Recommended Posts

Hello, I have a problem with this function.
I want that when a button is pressed, the corresponding piece will disappear. (works)
The problem is that this can only be seen by oneself, since doing a test with a friend, he saw my vehicle normally.

I don't know what's wrong, but I'm sure others can see it, as the wiki example works for all players.

(By the way, I don't know if I'm using the right topic, it's my first time posting.)

This is part of my code, clientside only. 

function sacarColinF() -- This function disappears the component
local theVehicle = getPedOccupiedVehicle(localPlayer)
     if (theVehicle) then
          setVehicleComponentVisible(theVehicle, "colin" , false)
     end
end
addEventHandler("onClientGUIClick", sacarColin, sacarColinF)
 
function ponerColinF() -- This function returns the visibility to the previous component
local theVehicle = getPedOccupiedVehicle(localPlayer)
     if (theVehicle) then
          setVehicleComponentVisible(theVehicle, "colin" , true)
     end
end
addEventHandler("onClientGUIClick", ponerColin, ponerColinF)

I don't know if 'localPlayer' has something to do with it, but changing it to 'source' or 'thePlayer' causes the script to not work correctly.

Link to comment

You are doing everything in the client, that will make the changes only affect the local player

What you need is to send from the server to all the players/clients the information of the vehicle and the component

client

function sacarColinF() -- This function disappears the component
local theVehicle = getPedOccupiedVehicle(localPlayer)
     if (theVehicle) then
        -- We send the information to the server and the server will send the information to all clients
        triggerServerEvent("setComponentVisible", localPlayer, theVehicle, "colin" , false)
     end
end
addEventHandler("onClientGUIClick", sacarColin, sacarColinF)
 
function ponerColinF() -- This function returns the visibility to the previous component
local theVehicle = getPedOccupiedVehicle(localPlayer)
     if (theVehicle) then
         -- We send the information to the server and the server will send the information to all clients
         triggerServerEvent("setComponentVisible", localPlayer, theVehicle, "colin" , true)
     end
end
addEventHandler("onClientGUIClick", ponerColin, ponerColinF)

-- This event is what the server sends to all clients to make the component visible / invisible
addEvent("setComponentVisible", true)
addEventHandler("setComponentVisible", root, 
   function(vehicle, component, visible)
      setVehicleComponentVisible(vehicle, component, visible)
   end 
)
-- This event is sent by the server to the player who has just connected so that he can see the changes in the vehicles

addEvent("setVehicleComponentsVisible", true)
addEventHandler("setVehicleComponentsVisible", root,
   function(vehicles)
      for vehicle, components in pairs(vehicles) do 
         if isElement(vehicle) then 
            for component, visible in pairs(components) do 
                setVehicleComponentVisible(vehicle, component, visible)
            end 
         end 
      end 
   end 
)

 

 

server

 

local vehicles = {} --save changes to vehicles in this table for players joining the server

addEvent("setComponentVisible", true)
addEventHandler("setComponentVisible", root, 
   function(vehicle, component, visible)
      if not vehicles[vehicle] then 
         vehicles[vehicle] = {}
      end 
      if not vehicles[vehicle][component] then 
          vehicles[vehicle][component] = visible
      end 
      triggerClientEvent(root, "setComponentVisible", root, vehicle, component, visible)
   end 
)
-- When a player enters the server they will not see the changes that were made before connecting. the server will send the information to that client so that it can see the changes
addEventHandler("onPlayerJoin", root,
   function()
      triggerClientEvent(source, "setVehicleComponentsVisible", source, vehicles)
   end 
)

 

Edited by alex17"
Link to comment
1 hour ago, alex17" said:

You are doing everything in the client, that will make the changes only affect the local player

What you need is to send from the server to all the players/clients the information of the vehicle and the component

client

function sacarColinF() -- This function disappears the component
local theVehicle = getPedOccupiedVehicle(localPlayer)
     if (theVehicle) then
        -- We send the information to the server and the server will send the information to all clients
        triggerServerEvent("setComponentVisible", localPlayer, theVehicle, "colin" , false)
     end
end
addEventHandler("onClientGUIClick", sacarColin, sacarColinF)
 
function ponerColinF() -- This function returns the visibility to the previous component
local theVehicle = getPedOccupiedVehicle(localPlayer)
     if (theVehicle) then
         -- We send the information to the server and the server will send the information to all clients
         triggerServerEvent("setComponentVisible", localPlayer, theVehicle, "colin" , true)
     end
end
addEventHandler("onClientGUIClick", ponerColin, ponerColinF)

-- This event is what the server sends to all clients to make the component visible / invisible
addEvent("setComponentVisible", true)
addEventHandler("setComponentVisible", root, 
   function(vehicle, component, visible)
      setVehicleComponentVisible(vehicle, component, visible)
   end 
)
-- This event is sent by the server to the player who has just connected so that he can see the changes in the vehicles

addEvent("setVehicleComponentsVisible", true)
addEventHandler("setVehicleComponentsVisible", root,
   function(vehicles)
      for vehicle, components in pairs(vehicles) do 
         if isElement(vehicle) then 
            for component, visible in pairs(components) do 
                setVehicleComponentVisible(vehicle, component, visible)
            end 
         end 
      end 
   end 
)

 

 

server

 

local vehicles = {} --save changes to vehicles in this table for players joining the server

addEvent("setComponentVisible", true)
addEventHandler("setComponentVisible", root, 
   function(vehicle, component, visible)
      if not vehicles[vehicle] then 
         vehicles[vehicle] = {}
      end 
      if not vehicles[vehicle][component] then 
          vehicles[vehicle][component] = visible
      end 
      triggerClientEvent(root, "setComponentVisible", root, vehicle, component, visible)
   end 
)
-- When a player enters the server they will not see the changes that were made before connecting. the server will send the information to that client so that it can see the changes
addEventHandler("onPlayerJoin", root,
   function()
      triggerClientEvent(source, "setVehicleComponentsVisible", source, vehicles)
   end 
)

 

It worked, thank you very much.

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