Perfect Posted December 9, 2013 Share Posted December 9, 2013 hi all, i am trying to change deagle to a gun which destroy vehicle which we shoot at. i am trying to change that gun by command "dgun". so when we type dgun, it will change to destroying gun and when we type dgun again it will turn to normal gun. but unfortunately my code is not working, hope you guys can help me. here is the code function Dgun ( attacker, weapon, bodypart, loss) if ( weapon == 24 ) then outputChatBox("Server: D-gun On!",source,0,255,0,true) for i, source in ipairs ( getElementsByType("player") ) target = getPlayerTarget ( source ) if ( target ) then if ( getElementType ( target ) == "vehicle" ) then destroyElement ( target ) outputChatBox("Server: Destroyed!!",source,0,255,0,true) end end end end end addEventHandler ( "onVehicleDamage", getRootElement (), Dgun ) addCommandHandler("dgun",Dgun) Link to comment
manve1 Posted December 9, 2013 Share Posted December 9, 2013 local dgunStatus = false function Dgun ( attacker, weapon, bodypart, loss) if dgunStatus == false then dgunStatus = true; if ( weapon == 24 ) then outputChatBox("Server: D-gun On!",source,0,255,0,true) for i, source in ipairs ( getElementsByType("player") ) target = getPlayerTarget ( source ) if ( target ) then if ( getElementType ( target ) == "vehicle" ) then destroyElement ( target ) outputChatBox("Server: Destroyed!!",source,0,255,0,true) end end end end elseif dgunStatus == true then dgunStatus = false return false; end addEventHandler ( "onVehicleDamage", getRootElement (), Dgun ) addCommandHandler("dgun",Dgun) Link to comment
Perfect Posted December 9, 2013 Author Share Posted December 9, 2013 its client side or server side ? and i tried both client and server side. it seems to not working, i change some things in script as per debugscript says here is the code.(no errors and no warnings and not working) function Dgun ( attacker, weapon, bodypart, loss) if dgunStatus == false then dgunStatus = true; if ( weapon == 24 ) then outputChatBox("Server: D-gun On!",source,0,255,0,true) for i, source in ipairs ( getElementsByType("player") ) do target = getPedTarget ( source ) if ( target ) then if ( getElementType ( target ) == "vehicle" ) then destroyElement ( target ) outputChatBox("Server: Destroyed!!",source,0,255,0,true) end end end end elseif dgunStatus == true then dgunStatus = false return false; end end addEventHandler ( "onVehicleDamage", getRootElement (), Dgun ) addCommandHandler("dgun",Dgun) Link to comment
TAPL Posted December 9, 2013 Share Posted December 9, 2013 Your code make no sense. You should use the event onClientPlayerWeaponFire and the parameter hitElement, trigger to server side to destroy the vehicle. Link to comment
Perfect Posted December 9, 2013 Author Share Posted December 9, 2013 so you mean this will work ? client function Dgun ( weapon, ammo, ammoInClip, hitX, hitY, hitZ, hitElement) if dgunStatus == false then dgunStatus = true; if weapon == 24 and getElementType(hitElement)=="vehicle" then outputChatBox("Server: D-gun On!",source,0,255,0,true) destroyElement ( target ) outputChatBox("Server: Destroyed!!",source,0,255,0,true) end else outputChatBox("Server: Destroyed!!",source,0,255,0,true) end end elseif dgunStatus == true then dgunStatus = false return false; addEventHandler ( "onClientPlayerWeaponFire", getLocalPlayer(), Dgun ) addCommandHandler("dgun",Dgun) Link to comment
TAPL Posted December 9, 2013 Share Posted December 9, 2013 You can't put a shit in the code and expect it to work. Link to comment
Perfect Posted December 9, 2013 Author Share Posted December 9, 2013 damn, can you please give me a code ? (note that please) Link to comment
TAPL Posted December 9, 2013 Share Posted December 9, 2013 Client Side: function Dgun(weapon, ammo, ammoInClip, hitX, hitY, hitZ, hitElement) if weapon == 24 and hitElement and dgunStatus then if getElementType(hitElement) == "vehicle" then -- trigger goes here. end end end addEventHandler("onClientPlayerWeaponFire", getLocalPlayer(), Dgun ) addCommandHandler("dgun", function() dgunStatus = not dgunStatus outputChatBox("Server: D-gun "..(dgunStatus and "On" or "Off").."!", 0, 255, 0, true) end) Server Side: -- Your turn to do it. Link to comment
Perfect Posted December 9, 2013 Author Share Posted December 9, 2013 ok. first, thnx for the code. and i tried my best as what you said. (btw this is my first time using trigger events) client function Dgun(weapon, ammo, ammoInClip, hitX, hitY, hitZ, hitElement) if weapon == 24 and hitElement and dgunStatus then if getElementType(hitElement) == "vehicle" then triggerServerEvent ( "devent", localPlayer) end end end addEventHandler("onClientPlayerWeaponFire", getLocalPlayer(), Dgun ) addCommandHandler("dgun", function() dgunStatus = not dgunStatus outputChatBox("Server: D-gun "..(dgunStatus and "On" or "Off").."!", 0, 255, 0, true) end) server function dgevent(weapon, ammo, ammoInClip, hitX, hitY, hitZ, hitElement) -- i am confused about this part. i have parameter in client side, so if i am trigger event then i must put those parameters in server side ?. destroyVehicle(hitElement) end addEvent("devent", true) addEventHandler("devent", root, dgevent) -- and here what is root,getRootElement() and sound. i mean what will happen if i put one of them ? i have got some questions too (see server file), hope you will help again Link to comment
TAPL Posted December 9, 2013 Share Posted December 9, 2013 You must pass hitElement in the trigger from the client side to the server side. Where do destroyVehicle come from? root is predefined variable of getRootElement(). Link to comment
WASSIm. Posted December 9, 2013 Share Posted December 9, 2013 function Dgun(weapon, ammo, ammoInClip, hitX, hitY, hitZ, hitElement) if weapon == 24 and hitElement and dgunStatus then if getElementType(hitElement) == "vehicle" then destroyElement(hitElement) end end end addEventHandler("onClientPlayerWeaponFire", getLocalPlayer(), Dgun ) addCommandHandler("dgun", function() dgunStatus = not dgunStatus outputChatBox("Server: D-gun "..(dgunStatus and "On" or "Off").."!", 0, 255, 0, true) end) Link to comment
TAPL Posted December 9, 2013 Share Posted December 9, 2013 Vehicle created from server side can't be destroyed from the client side. @Perfect, check the wiki example, the example pass the string "Hello world" from the client to the server and then output it, in your case you will pass hitElement from the client side to the server side then destroy it, simply? https://wiki.multitheftauto.com/wiki/TriggerServerEvent Link to comment
Sasu Posted December 9, 2013 Share Posted December 9, 2013 Client: function Dgun(weapon, ammo, ammoInClip, hitX, hitY, hitZ, hitElement) if weapon == 24 and hitElement and dgunStatus then if getElementType(hitElement) == "vehicle" then triggerServerEvent("onRequestVehicleDestroy", localPlayer, hitElement) end end end addEventHandler("onClientPlayerWeaponFire", getLocalPlayer(), Dgun ) addCommandHandler("dgun", function() dgunStatus = not dgunStatus outputChatBox("Server: D-gun "..(dgunStatus and "On" or "Off").."!", 0, 255, 0, true) end) Server: addEvent("onRequestVehicleDestroy", true) addEventHandler("onRequestVehicleDestroy", root, function(v) if v and isElement(v) then destroyElement(v) outputChatBox("Server: Destroyed!!", source, 0, 255, 0, true) end end) Link to comment
Perfect Posted December 9, 2013 Author Share Posted December 9, 2013 Guys Thnx so much!!! @TAPL Thnx for insisting me to learn how to trigger events. @Sasuke Thnx for Giving me the whole and final code. I really appreciate your help guys and i am gonna analyse that script and going to use in other scripts! Thank you (and btw @TAPL, i laughed so much when you said "You can't put a :~ in the code and expect it to work.". still laughing when remembering ) 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