1- local spam = {}
1+ local spam = 0
22
3- function initBind (thePlayer )
4- if (thePlayer == localPlayer and getVehicleType (source ) == " Train" ) then
5- bindKey (" H " , " down" , soundHorn )
3+ function initBind (theVehicle )
4+ if (getVehicleType (theVehicle ) == " Train" ) then
5+ bindKey (" horn " , " down" , soundHorn )
66 end
77end
8- addEventHandler (" onClientVehicleEnter " , getRootElement () , initBind )
8+ addEventHandler (" onClientPlayerVehicleEnter " , localPlayer , initBind )
99
1010function soundHorn ()
1111 local vehicle = getPedOccupiedVehicle (localPlayer )
12-
13- if getVehicleType (vehicle ) ~= " Train" then return end
14-
1512 -- Make it so they can't play multiple train horns at the same time (causes lag and distortion)
16- if spam [localPlayer ] and getTickCount () - spam [localPlayer ] < 5000 then
17- return
18- end
19-
20- if (vehicle and getVehicleController (vehicle ) == localPlayer ) then
21- spam [localPlayer ] = getTickCount ()
22- x , y , z = getElementPosition (vehicle )
23- triggerServerEvent (" onSyncHorn" , getRootElement (), localPlayer , vehicle )
24- sound = playSound3D (" horn.aac" , x , y , z , false )
25- setSoundVolume (sound , 1.0 )
13+ if (vehicle and getVehicleType (vehicle ) == " Train" and getVehicleController (vehicle ) == localPlayer and getTickCount () - spam >= 5000 ) then
14+ spam = getTickCount ()
15+ triggerServerEvent (" onSyncHorn" , resourceRoot )
16+ local x , y , z = getElementPosition (vehicle )
17+ local sound = playSound3D (" horn.aac" , x , y , z , false )
2618 attachElements (sound , vehicle )
2719 setSoundMaxDistance (sound , 250 )
2820 end
2921end
3022
3123function syncedHorn (train , x , y , z )
32- if (isElement (train ) and getVehicleType (train ) == " Train" ) then
33- sound = playSound3D (" horn.aac" , x , y , z , false )
34- setSoundVolume (sound , 1.0 )
24+ if (isElement (train ) and getVehicleType (train ) == " Train" and source ~= localPlayer ) then
25+ local sound = playSound3D (" horn.aac" , x , y , z , false )
3526 attachElements (sound , train )
3627 setSoundMaxDistance (sound , 250 )
3728 end
3829end
3930addEvent (" onPlaySyncedHorn" , true )
40- addEventHandler (" onPlaySyncedHorn" , localPlayer , syncedHorn )
31+ addEventHandler (" onPlaySyncedHorn" , root , syncedHorn )
4132
42- function cleanUp (thePlayer )
43- if (thePlayer == localPlayer and getVehicleType (source ) == " Train" ) then
44- unbindKey (" H " , " down" , soundHorn )
33+ function cleanUp (theVehicle )
34+ if (getVehicleType (theVehicle ) == " Train" ) then
35+ unbindKey (" horn " , " down" , soundHorn )
4536 end
4637end
47- addEventHandler (" onClientVehicleExit " , getRootElement () , cleanUp )
38+ addEventHandler (" onClientPlayerVehicleExit " , localPlayer , cleanUp )
4839
49- -- Dying in train, so not triggering onClientVehicleExit
40+ -- Dying in train, so not triggering onClientPlayerVehicleExit
5041function integrityCheck ()
5142 local vehicle = getPedOccupiedVehicle (localPlayer )
5243
5344 if (vehicle and getVehicleType (vehicle ) == " Train" ) then
54- unbindKey (" H " , " down" , soundHorn )
45+ unbindKey (" horn " , " down" , soundHorn )
5546 end
5647end
5748addEventHandler (" onClientPlayerWasted" , localPlayer , integrityCheck )
0 commit comments