Jump to content

[HELP] createWeapon Sync


Recommended Posts

I know I have asked this before, but I have tried it many times and what was suggested didn't work, so here I am asking again, can someone please show me how to make the createWeapon function sync with the server?

Here is the rough shooting code I have for the client side:

  
theWeapon = nil; 
function onClientResourceStart() 
    theWeaponID = 362; 
    if (theWeaponID == 362) then 
        thePlayer = getLocalPlayer(); 
        outputChatBox("A minigun will be added to your vehicle...") 
        theCar = getPedOccupiedVehicle(thePlayer); 
        local x, y, z = getElementPosition(theCar); 
        local hisWeapon = createWeapon("minigun", x, y, z); 
        if ((theCar) and (hisWeapon)) then 
            attachElements(hisWeapon, theCar, 0, 0, 0); 
            setElementRotation(hisWeapon, 2, 45, 123); 
            --theWeaponFireTimer = setTimer(fireVehicleWeapon, 50, 0, true); 
            setWeaponOwner(hisWeapon, thePlayer); 
            setElementData(hisWeapon, "owner", thePlayer); 
            setElementData(thePlayer, "theGun", hisWeapon); 
            setWeaponAmmo(hisWeapon, 500); 
            if (thePlayer == getLocalPlayer()) then 
                outputChatBox("Your minigun has been enabled!"); 
                --bindKey("lctrl", "down", shootWeapon, hisWeapon, true) 
                theWeapon = hisWeapon; 
            end 
        end 
    elseif (theWeaponID == 359) then 
     
    end  
end 
addEventHandler("onClientResourceStart", getRootElement(), onClientResourceStart); 
addCommandHandler("createWeapon", attachWeaponToVehicle); 
  
function onClientPreRender() 
    if (getKeyState("lctrl") == true) then 
        shootWeapon(theWeapon, true); 
    end 
end 
addEventHandler("onClientPreRender", getRootElement(), onClientPreRender); 
  
--function shootWeapon(theKey, theKeyState, theWeapon, isSoundEnabled) 
function shootWeapon(theWeapon, isSoundEnabled) 
    --outputChatBox("FIRING!"); 
    --if (theKeyState == "down") then 
        if (theWeapon) then 
             
            --local rx, ry, rz = getElementRotation(theCar); 
            --setElementRotation(theWeapon, rx, ry, rz); 
            fireWeapon(theWeapon); 
            if (isSoundEnabled == true) then 
                local x, y, z = getElementPosition(theWeapon); 
                theWeaponSound = playSound3D("minigun_base.mp3", x, y, z, false); 
            end 
        else 
            --removeVehicleWeapon(); 
        end 
    --end 
end 
  

Link to comment

Hey guys, is there any better way I can do this? this causes the game to freeze due to the for loop!

  
function shootTheWeapon(thePlayer) 
    for weaponKey, weaponValue in pairs(theWeapons) do 
        if (getElementData(weaponValue, "owner") == thePlayer) then 
            shootWeapon(weaponValue, true); 
        end 
    end 
end 
addEvent("shootTheWeapon", true); 
addEventHandler("shootTheWeapon", getRootElement(), shootTheWeapon); 
  

I have done this method because sending the weapon as an argument to the server doesn't work, the server receives it as a nil object.

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