Jump to content

sync bug (setElementData)


Xwad

Recommended Posts

Hi! This script makes possible to play a 3d sound (looped sound) by pressing the CTRL button, and stop the sound when the CTRL button is "up". Well, it has a really annoying sync bug. As you can see, there is a setElementData function in my client side script, that stores the sound element. When the player stops pressing the CTRL button then the script stops the sound with the stored element. Well the problem is that when another player press the CTRL button then the setElementData will be overwritted, and will cause really strange, annoying bugs.

So the bug is: when player1 press CTRL, and after that player2 also press CTRL then the sound for player1 will not stop anymore, because player2 owerwrited the setElementData, when he pressed CTRL. Well, its not easy to explain, so i hope you understood me.

Is the any way to avoid this owerwriting? or do i have to do this another way, without setElementData? Thanks in advance.

 

 

client

--start sound
function start_fire_sound()
if isPedInVehicle (localPlayer) then
local veh = getPedOccupiedVehicle(localPlayer)
if getElementModel(veh) == 476 then
triggerServerEvent ( "start_fire_sound", resourceRoot, veh ) 
end
end
end
bindKey("lctrl", "down", start_fire_sound)

function start_fire_c(veh)			
local x,y,z = getElementPosition (veh)	
local sound_fire = playSound3D("files/fire.wav",x,y,z, true)
setSoundMaxDistance( sound_fire, 500 )
setSoundVolume(sound_fire, 1)
attachElements ( sound_fire, veh, 0,0,0 )

setElementData(localPlayer, "sound_fire", sound_fire)
end
addEvent( "start_fire_sound", true )
addEventHandler( "start_fire_sound", localPlayer, start_fire_c )


--stop sound
function stop_fire_sound()
if isPedInVehicle (localPlayer) then
local veh = getPedOccupiedVehicle(localPlayer)
if getElementModel(veh) == 476 then
triggerServerEvent ( "stop_fire_sound", resourceRoot, veh ) 
end
end
end
bindKey("lctrl", "up", stop_fire_sound)

function stop_fire_sound_c(veh)		
local x,y,z = getElementPosition (veh)	
local sound_fire_lastshot = playSound3D("files/fire_lastshot.wav",x,y,z, false)
setSoundMaxDistance( sound_fire_lastshot, 500 )
setSoundVolume(sound_fire_lastshot, 1)
attachElements ( sound_fire_lastshot, veh, 0,0,0 )

local sound_fire = getElementData(localPlayer, "sound_fire")
stopSound(sound_fire)
end
addEvent( "stop_fire_sound", true )
addEventHandler( "stop_fire_sound", localPlayer, stop_fire_sound_c )

 

server

function start_fire_sound(veh)
triggerClientEvent ("start_fire_sound", getRootElement(), veh)
end
addEvent( "start_fire_sound", true )
addEventHandler( "start_fire_sound", getRootElement(), start_fire_sound )


function stop_fire_sound(veh)
triggerClientEvent ("stop_fire_sound", getRootElement(), veh)
end
addEvent( "stop_fire_sound", true )
addEventHandler( "stop_fire_sound", getRootElement(), stop_fire_sound )

 

Link to comment

One of errors and mistakes in your code ; 

 

triggerServerEvent ( "start_fire_sound", resourceRoot, veh ) 

 

So for the sync i'm pretty sure because of the 'resourceRoot' use 'localPlayer' instead of that.

from the server side you are triggering to root and the wiki syntax says ; 

 

bool triggerClientEvent ( [table/element sendTo=getRootElement()], string name, element sourceElement, [arguments...] )

 

 

Edited by FaHaD
Link to comment

Try my code and inform me the results.

--Client side:

--start sound
function start_fire_sound ( )
	if isPedInVehicle ( localPlayer ) then
		local theVehicle = getPedOccupiedVehicle ( localPlayer )
		if ( theVehicle and getElementModel ( theVehicle ) == 476 ) then 
			triggerServerEvent ( "start_fire_sound", localPlayer, theVehicle ) 
		end
	end
end
bindKey ( "lctrl", "down", start_fire_sound )
 
addEvent ( "return_start_fire_sound", true )
addEventHandler ( "return_start_fire_sound", root, 
	function ( )
		local x, y, z = getElementPosition ( source )
		local fireSound = playSound3D ( "files/fire.wav", x, y, z, true ) 
		setSoundMaxDistance ( fireSound, 500 )
		setSoundVolume ( fireSound, 1 )
		attachElements ( fireSound, source, 0, 0, 0 )
		setElementData ( localPlayer, "sound_fire", fireSound )
	end
)
 
--stop sound
function stop_fire_sound ( )
	if isPedInVehicle ( localPlayer ) then
		local theVehicle = getPedOccupiedVehicle ( localPlayer )
		if ( theVehicle and getElementModel ( theVehicle ) == 476 ) then
			triggerServerEvent ( "stop_fire_sound", localPlayer, theVehicle ) 
		end
	end
end
bindKey ( "lctrl", "up", stop_fire_sound )
 
addEvent( "return_stop_fire_sound", true )
addEventHandler( "return_stop_fire_sound", root,
	function ( )
		local x, y, z = getElementPosition ( source )	
		local sound_fire_lastshot = playSound3D ( "files/fire_lastshot.wav", x, y, z, true )
		setSoundMaxDistance ( sound_fire_lastshot, 500 )
		setSoundVolume ( sound_fire_lastshot, 1 )
		attachElements ( sound_fire_lastshot, veh, 0,0,0 )
		local sound_fire = getElementData ( localPlayer, "sound_fire" )
		if ( sound_fire ) then
			stopSound ( sound_fire )
			destroyElement ( sound_fire )
		end
	end
)

 

--Server side:

addEvent ( "start_fire_sound", true )
addEventHandler ( "start_fire_sound", root, 
	function ( theVehicle )
		triggerClientEvent ( "return_start_fire_sound", theVehicle )
	end
)
 
addEvent ( "stop_fire_sound", true )
addEventHandler ( "stop_fire_sound", root, 
	function ( theVehicle )
		triggerClientEvent ( "return_stop_fire_sound", theVehicle )
	end
)

 

Link to comment

Change the old client code by this:

--start sound
function start_fire_sound ( )
	if isPedInVehicle ( localPlayer ) then
		local theVehicle = getPedOccupiedVehicle ( localPlayer )
		if ( theVehicle and getElementModel ( theVehicle ) == 476 ) then 
			triggerServerEvent ( "start_fire_sound", localPlayer, theVehicle ) 
		end
	end
end
bindKey ( "lctrl", "down", start_fire_sound )

local fireSound = { }
local lastShot = { }

addEvent ( "return_start_fire_sound", true )
addEventHandler ( "return_start_fire_sound", root, 
	function ( )
		if isElement ( fireSound [ localPlayer ] ) then
			destroyElement ( fireSound [ localPlayer ] ) 
		end
		local x, y, z = getElementPosition ( source )
		fireSound [ localPlayer ] = playSound3D ( "files/fire.wav", x, y, z, true )
		setSoundVolume ( fireSound [ localPlayer ], 1 )
		setSoundMaxDistance ( fireSound [ localPlayer ], 500 )
		attachElements ( fireSound [ localPlayer ], source, 0, 0, 0 ) 
	end
)
 
--stop sound
function stop_fire_sound ( )
	if isPedInVehicle ( localPlayer ) then
		local theVehicle = getPedOccupiedVehicle ( localPlayer )
		if ( theVehicle and getElementModel ( theVehicle ) == 476 ) then
			triggerServerEvent ( "stop_fire_sound", localPlayer, theVehicle ) 
		end
	end
end
bindKey ( "lctrl", "up", stop_fire_sound )
 
addEvent( "return_stop_fire_sound", true )
addEventHandler( "return_stop_fire_sound", root,
	function ( )
		if isElement ( lastShot [ localPlayer ] ) then
			destroyElement ( lastShot [ localPlayer ] ) 
		end
		
		if isElement ( fireSound [ localPlayer ] ) then
			destroyElement ( fireSound [ localPlayer ] ) 
		end
		local x, y, z = getElementPosition ( source )	
		lastShot [ localPlayer ] = playSound3D ( "files/fire_lastshot.wav", x, y, z, true )
		setSoundVolume ( lastShot [ localPlayer ], 1 )
		setSoundMaxDistance ( lastShot [ localPlayer ], 500 )
		attachElements ( lastShot [ localPlayer ], source, 0,0,0 ) 
	end
)

 

Link to comment

Try this ;

 

On client ;

 

aFireSound = {  };
aLastSound = {  };
addEvent ( 'onPlayFireSound',true );
addEventHandler ( 'onPlayFireSound',root,
  function ( aVehicle,aX,aY,aZ )
      if aVehicle and aX and aY and aZ then
      if aLastSound [ aVehicle ] and isElement ( aLastSound [ aVehicle ] ) then
        destroyElement ( aLastSound [ aVehicle ] )
        aLastSound [ aVehicle ] = nil
      end 
         aFireSound [ aVehicle ] = playSound3D ( 'files/fire.wav',aX,aY,aZ,false );
        setSoundMaxDistance ( aFireSound [ aVehicle ], 500 );
        setSoundVolume ( aFireSound [ aVehicle ], 1 );
        attachElements ( aFireSound [ aVehicle ],aVehicle,0,0,0 );
        setElementData ( aVehicle,'aSoundOn',true );
    end
end
);

addEvent ( 'onStopFireSound',true );
addEventHandler ( 'onStopFireSound',root,
  function ( aVehicle,aX,aY,aZ )
      if aVehicle and aX and aY and aZ then 
      if aFireSound [ aVehicle ] and isElement ( aFireSound [ aVehicle ] ) then
        destroyElement ( aFireSound [ aVehicle ] )
        aFireSound [ aVehicle ] = nil
      end
         aLastSound [ aVehicle ] = playSound3D ( 'files/fire_lastshot.wav',aX,aY,aZ,false );
        setSoundMaxDistance ( aLastSound [ aVehicle ], 500 );
        setSoundVolume ( aLastSound [ aVehicle ], 1 );
        attachElements ( aLastSound [ aVehicle ],aVehicle,0,0,0 );
        setElementData ( aVehicle,'aSoundOn',false );
    end
end
);

 

 

On server ;

 

function aMakeSoundPlay ( aPlayer )
    if aPlayer and getElementType ( aPlayer ) == 'player' then
      if isPedInVehicle ( aPlayer ) then
        local aVehicle = getPedOccupiedVehicle( aPlayer );
        if aVehicle and getElementModel ( aVehicle ) == 476 then
          local aX,aY,aZ = getElementPosition ( aVehicle );
          triggerClientEvent ( root,'onPlayFireSound',root,aVehicle,aX,aY,aZ );
      end
    end
  end
end

function aMakeSoundStop ( aPlayer )
    if aPlayer and getElementType ( aPlayer ) == 'player' then
      if isPedInVehicle ( aPlayer ) then
        local aVehicle = getPedOccupiedVehicle( aPlayer );
        if aVehicle and getElementModel ( aVehicle ) == 476 then
        if getElementData ( aVehicle,'aSoundOn' ) == true then
          local aX,aY,aZ = getElementPosition ( aVehicle );
            triggerClientEvent ( root,'onStopFireSound',root,aVehicle,aX,aY,aZ );
        end
      end
    end
  end
end

addEventHandler ( 'onResourceStart',resourceRoot,
    function (    )
        for _,aPlayers in ipairs ( getElementsByType ( 'player' ) ) do
          bindKey ( aPlayers,'lctrl','down',aMakeSoundPlay );
          bindKey ( aPlayers,'lctrl','up',aMakeSoundStop );
      end
  end
);

addEventHandler ( 'onPlayerJoin',root,
  function (    )
      bindKey ( source,'lctrl','down',aMakeSoundPlay );
      bindKey ( source,'lctrl','up',aMakeSoundStop );
  end
);

 

Link to comment

i don't know what are you trying to do i just optimized the code reply if it dosent work.

  • Client side
local playeSound = {}

addEvent ( 'playSound',true );
addEventHandler ( 'playSound',root,
  function ( aVehicle,sound)
    if aVehicle and sound then
        if playeSound [ aVehicle ] and isElement ( playeSound [ aVehicle ] ) then
            destroyElement (playeSound [ aVehicle ])
            playeSound [ aVehicle ] = nil
        end 
		
        local position = Vector3(getElementPosition ( aVehicle ))
        playeSound [ aVehicle ] = playSound3D ( 'files/'..sound..'.wav',position.x,position.y,position.z,false)
        setSoundMaxDistance ( playeSound [ aVehicle ], 500 )
        setSoundVolume ( playeSound [ aVehicle ], 1 )
        attachElements ( playeSound [ aVehicle ],aVehicle,0,0,0 )
    end
end
)
  • Server side
function aMakeSoundPlay (aPlayer,key,keyState,sound)
   if aPlayer and getElementType ( aPlayer ) == 'player' then
       if isPedInVehicle ( aPlayer ) then
            local aVehicle = getPedOccupiedVehicle( aPlayer )
            local model = getElementModel ( aVehicle )
            if model  == 476 then
               triggerClientEvent ( root,"playSound",root,aVehicle,sound)
            end
        end
    end
end

addEventHandler ( 'onResourceStart',resourceRoot,
  function ()
     local players = getElementsByType ( 'player' ) 
     for i=1, #players do
        binKeyForAll(players[i])
     end
  end
)

addEventHandler ( 'onPlayerJoin',root,
  function ()
     binKeyForAll(source)
  end
)

function binKeyForAll(player)
   if player and isElement(player) then
        bindKey (player,'lctrl','down',aMakeSoundPlay,'fire')
        bindKey (player,'lctrl','up',aMakeSoundPlay,'fire_lastshot')
   end 
end 

 

Link to comment

I have a new problem. Do not get me wrong, the previous code works perfect. I just added some new functions in the code. So first i changed all playSound3D to createWeapon, and it worked perfect. But i added a rotation system that does not work. This rotating function makes possible to rotate the object called "object3" to that direction where the "misc_a" (vehicle component) and  the "misc_c" (vehicle component) is rotating at. The problem is that its not rotating the object. I hope you can help me again. Thanks in advance!

 

client

object3 = {};
mg1 = {};
mg2 = {};

addEvent ( 'fire_mg',true );
addEventHandler ( 'fire_mg',root,
  function ( aVehicle,aX,aY,aZ )
      if aVehicle and aX and aY and aZ then
	    object3 [ aVehicle ] = createObject ( 357, aX,aY,aZ)
        attachElements ( object3 [ aVehicle ], aVehicle, -0.05,10,3, 0,0,90 )
	    mg1 [ aVehicle ] = createWeapon("m4", aX,aY,aZ)
	    mg2 [ aVehicle ] = createWeapon("m4", aX,aY,aZ)
     	setWeaponFiringRate(mg1 [ aVehicle ], 20)
	    setWeaponFiringRate(mg2 [ aVehicle ], 20)
        setWeaponState(mg1 [ aVehicle ], "firing")
        setWeaponState(mg2 [ aVehicle ], "firing")
	    attachElements(mg1 [ aVehicle ], object3 [ aVehicle ], 0.2, 0.2, 0, 0, 0, 0)
	    attachElements(mg2 [ aVehicle ], object3 [ aVehicle ], 0.2, -0.3, 0, 0, 0, 0)			
	  
        setElementData ( aVehicle,'mgFireOn',true );
        
		local veh = getPedOccupiedVehicle(localPlayer)
		setElementData ( aVehicle,'object3',object3 [ aVehicle ] );
		
		addEventHandler("onClientRender",root,rotate_object)
    end
end
);

function rotate_object()
local veh = getPedOccupiedVehicle(localPlayer)
local _,_,rz = getVehicleComponentRotation(veh, "misc_a")
local rx,_,_ = getVehicleComponentRotation(veh, "misc_c")
triggerServerEvent("rotateWeapon", localPlayer, rx,rz)
end 

 

and the server side

function rotateWeapon(rx,rz)
outputChatBox("rotate_s")
local object3 = getElementData ( aVehicle, "object3" )
rx = (-rx)
setElementAttachedOffsets ( object3, 0, 5.25, 2.45, 0, rx+7, rz+96)
end
addEvent("rotateWeapon", true)
addEventHandler("rotateWeapon", root, rotateWeapon)


function fire ( aPlayer )
    if aPlayer and getElementType ( aPlayer ) == 'player' then
      if isPedInVehicle ( aPlayer ) then
        local aVehicle = getPedOccupiedVehicle( aPlayer );
        if aVehicle and getElementModel ( aVehicle ) == 592 then
          local aX,aY,aZ = getElementPosition ( aVehicle );
          triggerClientEvent ( root,'fire_mg',root,aVehicle,aX,aY,aZ );
      end
    end
  end
end


function bind_start()
for _,aPlayers in ipairs ( getElementsByType ( 'player' ) ) do
bindKey ( aPlayers,'mouse1','down',fire );
end
end
addEventHandler ( 'onResourceStart',resourceRoot,bind_start)

function bind_join()
bindKey ( aPlayers,'mouse1','down',fire );
end
addEventHandler ( 'onPlayerJoin',root,bind_join )

 

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