Xwad Posted August 26, 2016 Share Posted August 26, 2016 When i exit the vehicle then the weapons are not destroyed:/ I got no debug:/ cient function onEnter() local veh = getPedOccupiedVehicle(localPlayer) if getElementModel(veh) == 593 then toggleControl ( "brake_reverse", true ) triggerServerEvent ( "createMG_s", localPlayer, veh ) end end addEventHandler("onClientVehicleEnter", getRootElement(), onEnter) function createMG(veh) if not isElement(mg) then local x,y,z = getElementPosition(veh) local mg = createWeapon("m4", x,y,z+3) local mg2 = createWeapon("m4", x,y,z+3) outputChatBox("createMG",255,255,215) setWeaponFiringRate(mg, 20) setWeaponFiringRate(mg2, 20) setWeaponClipAmmo(mg, 99999) setWeaponClipAmmo(mg2, 99999) setWeaponState(mg, "ready") setWeaponState(mg2, "ready") attachElements(mg, veh, 2,0.7,-0.5,0,2,90) --right side & - = right direction attachElements(mg2, veh, -1.9,0.7,-0.5,0,2,90) setElementData(localPlayer,"mg",mg) end end addEvent( "createMG_c", true ) addEventHandler( "createMG_c", localPlayer, createMG ) function onExit() local veh = getPedOccupiedVehicle(localPlayer) if getElementModel(veh) == 593 then local mg = getElementData(localPlayer,"mg",mg) local mg2 = getElementData(localPlayer,"mg2",mg2) triggerServerEvent ( "destroyMG_s", resourceRoot, mg,mg2 ) end end addEventHandler("onClientVehicleStartExit", getRootElement(), onExit) function destroyMG(mg,mg2) destroyElement(mg) destroyElement(mg2) end addEvent( "destroyMG_c", true ) addEventHandler( "destroyMG_c", localPlayer, destroyMG ) server function createMG_server(veh) triggerClientEvent ("createMG_c", getRootElement(), veh) end addEvent( "createMG_s", true ) addEventHandler( "createMG_s", getRootElement(), createMG_server ) function destroyMG_server(mg,mg2) triggerClientEvent ("destroyMG_c", getRootElement(), mg,mg2) end addEvent( "destroyMG_s", true ) addEventHandler( "destroyMG_s", getRootElement(), destroyMG_server ) Link to comment
idarrr Posted August 26, 2016 Share Posted August 26, 2016 I think you are destroying an Element Data, not the Object. Maybe you need to make table to store the Weapons. Link to comment
Dimos7 Posted August 26, 2016 Share Posted August 26, 2016 function onEnter() local veh = getPedOccupiedVehicle(localPlayer) if getElementModel(veh) == 593 then toggleControl ( "brake_reverse", true ) triggerServerEvent ( "createMG_s", localPlayer, veh ) end end addEventHandler("onClientVehicleEnter", getRootElement(), onEnter) function createMG(veh) if not isElement(mg) then local x,y,z = getElementPosition(veh) local mg = createWeapon("m4", x,y,z+3) local mg2 = createWeapon("m4", x,y,z+3) outputChatBox("createMG",255,255,215) setWeaponFiringRate(mg, 20) setWeaponFiringRate(mg2, 20) setWeaponClipAmmo(mg, 99999) setWeaponClipAmmo(mg2, 99999) setWeaponState(mg, "ready") setWeaponState(mg2, "ready") attachElements(mg, veh, 2,0.7,-0.5,0,2,90) --right side & - = right direction attachElements(mg2, veh, -1.9,0.7,-0.5,0,2,90) setElementData(localPlayer,"mg",mg) end end addEvent( "createMG_c", true ) addEventHandler( "createMG_c", localPlayer, createMG ) function onExit() local veh = getPedOccupiedVehicle(localPlayer) if getElementModel(veh) == 593 then local Mg = getElementData(localPlayer,"mg",mg) local Mg2 = getElementData(localPlayer,"mg2",mg2) triggerServerEvent ( "destroyMG_s", resourceRoot, mg,mg2 ) end end addEventHandler("onClientVehicleStartExit", getRootElement(), onExit) function destroyMG(mg,mg2) destroyElement(mg) destroyElement(mg2) end addEvent( "destroyMG_c", true ) addEventHandler( "destroyMG_c", localPlayer, destroyMG ) Link to comment
Gravestone Posted August 30, 2016 Share Posted August 30, 2016 The mg and mg2 variables are not global, means they can not be destroyed by another function. Use getAttachedElements maybe. Link to comment
idarrr Posted August 31, 2016 Share Posted August 31, 2016 Try this. Client local mg = {} -- Store weapon on table function onEnter() local veh = getPedOccupiedVehicle(localPlayer) if getElementModel(veh) == 593 then toggleControl ( "brake_reverse", true ) triggerServerEvent ( "createMG_s", localPlayer, veh ) end end addEventHandler("onClientVehicleEnter", getRootElement(), onEnter) function onExit() local veh = getPedOccupiedVehicle(localPlayer) if getElementModel(veh) == 593 then triggerServerEvent ( "destroyMG_s", resourceRoot, veh ) end end addEventHandler("onClientVehicleStartExit", getRootElement(), onExit) -- If you want to add weapon on table function createMG(veh) local x,y,z = getElementPosition(veh) -- If there's any table on that Vehicle then store the weapon if mg[veh] then mg[veh]["mg1"] = createWeapon("m4", x,y,z+3) mg[veh]["mg2"] = createWeapon("m4", x,y,z+3) setWeaponFiringRate(mg[veh]["mg1"], 20) setWeaponFiringRate(mg[veh]["mg2"], 20) setWeaponClipAmmo(mg[veh]["mg1"], 99999) setWeaponClipAmmo(mg[veh]["mg2"], 99999) setWeaponState(mg[veh]["mg1"], "ready") setWeaponState(mg[veh]["mg2"], "ready") attachElements(mg[veh]["mg1"], veh, 2,0.7,-0.5,0,2,90) attachElements(mg[veh]["mg2"], veh, -1.9,0.7,-0.5,0,2,90) -- If no table on that vehicle then create the table for it and store the weapon else mg[veh] = {} mg[veh]["mg1"] = createWeapon("m4", x,y,z+3) mg[veh]["mg2"] = createWeapon("m4", x,y,z+3) setWeaponFiringRate(mg[veh]["mg1"], 20) setWeaponFiringRate(mg[veh]["mg2"], 20) setWeaponClipAmmo(mg[veh]["mg1"], 99999) setWeaponClipAmmo(mg[veh]["mg2"], 99999) setWeaponState(mg[veh]["mg1"], "ready") setWeaponState(mg[veh]["mg2"], "ready") attachElements(mg[veh]["mg1"], veh, 2,0.7,-0.5,0,2,90) attachElements(mg[veh]["mg2"], veh, -1.9,0.7,-0.5,0,2,90) end end addEvent( "createMG_c", true ) addEventHandler( "createMG_c", localPlayer, createMG ) -- If you want to destroy the weapon function destroyMG(veh) -- If there's any table on that Vehicle if mg[veh] then -- Check if the weapons are exists on table if mg[veh]["mg1"] and mg[veh]["mg2"] then -- Destroy the element destroyElement(mg[veh]["mg1"]) destroyElement(mg[veh]["mg2"]) -- Clear the table mg[veh] = nil end end end addEvent( "destroyMG_c", true ) addEventHandler( "destroyMG_c", localPlayer, destroyMG ) -- If you want to check all Weapon on table function showMeBaby(player) for k, v in pairs(mg) do outputChatBox("Weapon on Vehicle "..getVehicleName(k), player) for i, w in pairs(v) do outputChatBox(i, player) end end end addCommandHandler("showme", showMeBaby) Server function createMG_server(veh) triggerClientEvent ("createMG_c", getRootElement(), veh) end addEvent( "createMG_s", true ) addEventHandler( "createMG_s", getRootElement(), createMG_server ) function destroyMG_server(veh) triggerClientEvent ("destroyMG_c", getRootElement(), veh) end addEvent( "destroyMG_s", true ) addEventHandler( "destroyMG_s", getRootElement(), destroyMG_server ) Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now