Jump to content

Vehicle components server-side


Recommended Posts

Hello!

Imagine that a vehicle has several body kits in the form of vehicle components. I can edit their visibility on the client side (setVehicleComponentVisible), but how do I do this on the server side for all players? Move client-side operations to server then back to client-side for all players? Or is there an easier way?
Or is it possible to turn components (body kits) into upgrades? (Which have shared function
addVehicleUpgrade)

Link to comment

Client side

addDebugHook ("postFunction", function(sourceResource, functionName, isAllowedByACL, luaFilename, luaLineNumber, ... )
	if sourceResource == getThisResource() then return end
	
	local args = {...}
	
	local veh = args[1]
	local comp = args[2]
	local visible = args[3]
	
	local compVisible = getElementData(veh, "component_visible") or {}
	
	compVisible[comp] = visible
	
	setElementData(veh, "component_visible", compVisible)
end, {"setVehicleComponentVisible"})

addEventHandler ("onClientElementStreamIn", root, function()
	if getElementType(source) ~= "vehicle" then return end
	
	local compVisible = getElementData(source, "component_visible") or {}
	
	for comp, visible in pairs (compVisible) do
		setVehicleComponentVisible (source, comp, visible)
	end
end)

addEventHandler ("onClientElementDataChange", root, function(db, _, visible)
	if db ~= "component_visible" then return end
	if getElementType (source) ~= "vehicle" then return end
	
	local compVisible = getElementData(source, "component_visible") or {}
	
	for comp, visible in pairs (compVisible) do
		setVehicleComponentVisible (source, comp, visible)
	end
end)

 

  • Thanks 1
Link to comment

Hope it works! I can't even test it with other players, but code looks RELIABLE, :D TY

Am I supposed to use this outside of the resource, where I change the visibility of components?

Link to comment
  • 2 weeks later...
On 15/06/2024 at 19:39, sadvely said:

Hope it works! I can't even test it with other players, but code looks RELIABLE, :D TY

Am I supposed to use this outside of the resource, where I change the visibility of components?

No, you can use this code in outside resource

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