Jump to content

triggerServerEvent does not work


Xwad

Recommended Posts

Everythink is just working fine. The problem is that the triggerServerEvent is not triggering to the server.

line: 43, 69, 75, 81, 87

 

CLIENT.LUA

function create_weapon(veh)
local x,y,z = getElementPosition(veh)
local obj = createObject ( 357, x,y,z)
attachElements ( obj, veh, 0,0,0,0,0,0 )

setElementData(localPlayer,"obj",obj)

local gun1 = createWeapon(30, x,y,z)
local gun2 = createWeapon(30, x,y,z)
local gun3 = createWeapon(30, x,y,z)
local gun4 = createWeapon(30, x,y,z)

setElementData(localPlayer,"gun1",gun1)
setElementData(localPlayer,"gun2",gun2)
setElementData(localPlayer,"gun3",gun3)
setElementData(localPlayer,"gun4",gun4)

attachElements ( gun1, obj, 1.18, 0.24, 0, 0, -5, 0)
attachElements ( gun2, obj, 1.18, -0.55, 0, 0, -5, 0)
attachElements ( gun3, obj, 1.18, -0.55, 0.35, 0, -5, 0)
attachElements ( gun4, obj, 1.18, 0.24, 0.35, 0, -5, 0)

setWeaponState(gun1, "ready")
setWeaponState(gun2, "ready")
setWeaponState(gun3, "ready")
setWeaponState(gun4, "ready")

addEventHandler ( "onClientPreRender", root, rotate_object )
end





function rotate_object()

local veh = getPedOccupiedVehicle ( localPlayer )
local obj = getElementData(localPlayer,"obj")

local _,_,rz = getVehicleComponentRotation(veh, "misc_a")
local rx,_,_ = getVehicleComponentRotation(veh, "misc_b")

triggerServerEvent("rotateWeapon", localPlayer, veh,obj,rx,rz) 
end 







fired1 = false
fired2 = true
fired3 = true
fired4 = true


function fire()
	if getKeyState( "mouse1" ) == true then
	
		local veh = getPedOccupiedVehicle(localPlayer)
		
		if isPedInVehicle(localPlayer) and getElementModel(veh) == 602 then	
		
			if fired1 == false then
				local gun1 = getElementData (localPlayer, "gun1")
				fired1 = true
				fired2 = false
				triggerServerEvent ( "fire_server1", localPlayer,veh,gun1)
			
			elseif fired2 == false then
				local gun2 = getElementData (localPlayer, "gun2")
				fired2 = true
				fired3 = false
				triggerServerEvent ( "fire_server2", localPlayer,veh,gun2)
				
			elseif fired3 == false then
				local gun3 = getElementData (localPlayer, "gun3")
				fired3 = true
				fired4 = false
				triggerServerEvent ( "fire_server3", localPlayer,veh,gun3)
			
			elseif fired4 == false then
				local gun4 = getElementData (localPlayer, "gun4")
				fired4 = true
				fired1 = false			
				triggerServerEvent ( "fire_server4", localPlayer,veh,gun4)
			end

			setTimer ( fire, 200, 1 )	 	
		end
	end
end
bindKey("mouse1", "down", fire) 







function fire_client(veh,gun1)
outputChatBox("fire_client1",2,1,10)
outputChatBox ( "gun1",255,0,100 )
fireWeapon(gun1)
fireWeapon(gun1)	
local x,y,z = getElementPosition(veh)				
local sound_gun1 = playSound3D("files/shoot.wav",x,y,z, false)
setSoundMaxDistance( sound_gun1, 500 )
setSoundVolume(sound_gun1, 1)
end
addEvent("fire_client", true)
addEventHandler("fire_client", root, fire_client)



function fire_client2(veh,gun2)
outputChatBox("fire_client2",2,1,10)
outputChatBox ( "gun2",255,0,100 )
fireWeapon(gun2)
fireWeapon(gun2)	
local x,y,z = getElementPosition(veh)				
local sound_gun2 = playSound3D("files/shoot.wav",x,y,z, false)
setSoundMaxDistance( sound_gun2, 500 )
setSoundVolume(sound_gun2, 1)
end
addEvent("fire_client2", true)
addEventHandler("fire_client2", root, fire_client2)



function fire_client3(veh,gun3)
outputChatBox("fire_client3",2,1,10)
outputChatBox ( "gun3",255,0,100 )
fireWeapon(gun3)
fireWeapon(gun3)	
local x,y,z = getElementPosition(veh)				
local sound_gun3 = playSound3D("files/shoot.wav",x,y,z, false)
setSoundMaxDistance( sound_gun3, 500 )
setSoundVolume(sound_gun3, 1)
end
addEvent("fire_client3", true)
addEventHandler("fire_client3", root, fire_client3)



function fire_client4(veh,gun4)
outputChatBox("fire_client4",2,1,10)
outputChatBox ( "gun4",255,0,100 )
fireWeapon(gun4)
fireWeapon(gun4)	
local x,y,z = getElementPosition(veh)				
local sound_gun4 = playSound3D("files/shoot.wav",x,y,z, false)
setSoundMaxDistance( sound_gun4, 500 )
setSoundVolume(sound_gun4, 1)
end
addEvent("fire_client4", true)
addEventHandler("fire_client4", root, fire_client4)

 

SERVER.LUA

function rotateWeapon(veh,obj,rx,rz)
rx = (-rx)
setElementAttachedOffsets ( obj, 0, 0, 0, 0, rx+7, rz+96)
end
addEvent("rotateWeapon", true)
addEventHandler("rotateWeapon", root, rotateWeapon)




function fire_server1(veh,gun1)
outputChatBox("fire_server1")
triggerClientEvent("fire_client1", getRootElement(), veh,gun1)
end
addEvent("fire_server1", true)
addEventHandler("fire_server1",getRootElement(), fire_server1)


function fire_server2(veh,gun1)
outputChatBox("fire_server2")
triggerClientEvent("fire_client2", getRootElement(), veh,gun2)
end
addEvent("fire_server2", true)
addEventHandler("fire_server2",getRootElement(), fire_server2)


function fire_server3(veh,gun1)
outputChatBox("fire_server3")
triggerClientEvent("fire_client3", getRootElement(), veh,gun3)
end
addEvent("fire_server3", true)
addEventHandler("fire_server3",getRootElement(), fire_server3)


function fire_server4(veh,gun1)
outputChatBox("fire_server4")
triggerClientEvent("fire_client4", getRootElement(), veh,gun4)
end
addEvent("fire_server4", true)
addEventHandler("fire_server4",getRootElement(), fire_server4)

 

Edited by Xwad
Link to comment

I know but I had plenty of situations where it simply doesn't work with local player for whatever reason.

€DIT: I believe resourceRoot is just as good but it saves some server CPU compared with root

 

€DIT2: in any case you need to pass these weapons in another way to the server, I suggest the weapon base where they are attached to, because unlike the weapons, the weapon base exists server side. Then on the client you simply use getAttachedElements() to find the weapon that is attached to that base and fire that gun GG

Edited by Einheit-101
Link to comment

About line 43, check if 'veh' is a vehicle element:


function rotate_object()
	local veh = getPedOccupiedVehicle ( localPlayer )
	if veh then
		local _,_,rz = getVehicleComponentRotation(veh, "misc_a")
		local rx,_,_ = getVehicleComponentRotation(veh, "misc_b")
		
		if getElementData(localPlayer,"obj") then
			triggerServerEvent("rotateWeapon", localPlayer, veh,getElementData(localPlayer,"obj"),rx,rz)
		end
	end
end

 

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