Jump to content

Synchronized side van doors


ivan8065

Recommended Posts

Hello, I am trying to do working side doors on van through setComponentPosition and trying to sync it with other players with triggerClientEvent but it's not working, what I have got wrong? Thank you for reply.

CLIENT

function scp()
        local theVeh = getPedOccupiedVehicle(localPlayer)
	local getComponent = getVehicleComponents(theVeh) -- returns table with all the components of the vehicle
        if (theVeh) then
		local x, y, z = getVehicleComponentPosition(theVeh, "bocne_dvere") --get the position of the component
                setVehicleComponentPosition(theVeh, "bocne_dvere", x+0.1, y-1, 0) -- increases by 1 unit
            end
        end
addEvent("scp", true)
addEventHandler("scp", localPlayer, scp)

function scp2()
        local theVeh = getPedOccupiedVehicle(localPlayer)
	local getComponent = getVehicleComponents(theVeh) -- returns table with all the components of the vehicle
        if (theVeh) then
		local x, y, z = getVehicleComponentPosition(theVeh, "bocne_dvere") --get the position of the component
                setVehicleComponentPosition(theVeh, "bocne_dvere", x-0.1, y+1, 0) -- increases by 1 unit
            end
        end
addEvent("scp2", true)
addEventHandler("scp2", localPlayer, scp2)

function scpreset()
        local theVeh = getPedOccupiedVehicle(localPlayer)
	local getComponent = getVehicleComponents(theVeh) -- returns table with all the components of the vehicle
        if (theVeh) then
		local x, y, z = getVehicleComponentPosition(theVeh, "bocne_dvere") --get the position of the component
                setVehicleComponentPosition(theVeh, "bocne_dvere", 0, 0, 0) -- increases by 1 unit
            end
        end
addEvent("scpreset", true)
addEventHandler("scpreset", localPlayer, scpreset)

SERVER

function scp ( thePlayer, commandName )
	local x, y, z = getElementPosition(thePlayer)
	local nearbyPlayers = getElementsWithinRange(x, y, z, 100, "player")
    triggerClientEvent (nearbyPlayers,"scp",thePlayer)
end
addCommandHandler ("scp", scp)

function scp2 ( thePlayer, commandName )
	local x, y, z = getElementPosition(thePlayer)
	local nearbyPlayers = getElementsWithinRange(x, y, z, 100, "player")
    triggerClientEvent (nearbyPlayers,"scp2",thePlayer)
end
addCommandHandler ("scp2", scp2)

function scpreset ( thePlayer, commandName )
	local x, y, z = getElementPosition(thePlayer)
	local nearbyPlayers = getElementsWithinRange(x, y, z, 100, "player")
    triggerClientEvent (nearbyPlayers,"scpreset",thePlayer)
end
addCommandHandler ("scpreset", scpreset)

 

Link to comment
  • Moderators
1 hour ago, ivan8065 said:
function scp()
        local theVeh = getPedOccupiedVehicle(localPlayer)

 

Try this:

function scp(thePlayer)
        local theVeh = getPedOccupiedVehicle(thePlayer)

 

Be aware that this code does not sync the state of the component, it only syncs the adjustment. But it is a good start, keep it up!

Link to comment

Hello ivan8065,

I would like to amend the statement by IIYAMA which I think does lead you in the right direction. The way that you have written your clientside code the "synchronization" is only happening for the player that requested the action, not all the players. This way it causes a desync where other players cannot see changing of the components between each other.

Consider changing...

...
addEventHandler("scp", localPlayer, scp)
...

to ...

...
addEventHandler("scp", root, scp)
...

I am not sure if IIYAMA is aware but the source variable in ivan8065's code event handlers is already containing the player, so there is no need to put a "thePlayer" parameter to the event handler. You should consider doing...

function scp()
        local theVeh = getPedOccupiedVehicle(source)

Also please consider naming your events strongly and picking different function names than the event names.

Keep up the good work with those interesting ideas!

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