Jump to content

createProjectile crash


Recommended Posts

Posted

I have created a resource for attaching missiles and bombs to vehicles, however the client of the player who has fired some projectiles sometimes crashes after the projectiles have exploded.

Server code:

  
function addVehicleBomb(vehicle, xoff, yoff, zoff, key) 
    local attachedBombs={} 
    if getElementData(vehicle,"bombObjs") then 
        attachedBombs=getElementData(vehicle,"bombObjs") 
    end 
    local bombObject = createObject(1636,0,0,0,0,0,0) 
    attachElements(bombObject,vehicle,xoff,yoff,zoff) 
    setElementData(bombObject,"bombAvailable","1") 
    setElementData(bombObject,"bombKey",key) 
    table.insert(attachedBombs,bombObject) 
    setElementData(vehicle,"bombObjs",attachedBombs) 
    triggerClientEvent("vehicleGunsChanged", getRootElement(), vehicle) 
end 
  
function removeVehicleBombs(vehicle) 
    if getElementData(vehicle,"bombObjs") then 
        attachedBombs=getElementData(vehicle,"bombObjs") 
        for index2,bomb in pairs(attachedBombs) do 
            destroyElement(bomb) 
            table.remove(attachedBombs,index2) 
        end 
    end 
end 
  
  
function addVehicleMissile(vehicle, xoff, yoff, zoff, key) 
    local attachedMissiles={} 
    if getElementData(vehicle,"missileObjs") then 
        attachedMissiles=getElementData(vehicle,"missileObjs") 
    end 
    local missileObject = createObject(3790,0,0,0,0,0,0) 
    attachElements(missileObject,vehicle,xoff,yoff,zoff,0,0,-90) 
    setElementData(missileObject,"missileAvailable","1") 
    setElementData(missileObject,"missileKey",key) 
    table.insert(attachedMissiles,missileObject) 
    setElementData(vehicle,"missileObjs",attachedMissiles) 
    triggerClientEvent("vehicleGunsChanged", getRootElement(), vehicle) 
end 
  
  
 addEventHandler( "onVehicleExplode", getRootElement( ), 
    function() 
        local attachedBombs=getElementData(source,"bombObjs") 
        if attachedBombs then 
            for index2,bomb in pairs(attachedBombs) do   
                setElementData(bomb,"bombAvailable","0") 
                setElementAlpha(bomb,0) 
            end      
        end 
        local attachedMissiles=getElementData(source,"missileObjs") 
        if attachedMissiles then 
            for index2,missile in pairs(attachedMissiles) do     
                setElementData(missile,"missileAvailable","0") 
                setElementAlpha(missile,0) 
            end      
        end 
    end 
) 
  
function onVehicleRespawn ( exploded ) 
    local attachedBombs=getElementData(source,"bombObjs") 
    if attachedBombs then 
        for index2,bomb in pairs(attachedBombs) do   
            setElementData(bomb,"bombAvailable","1") 
            setElementAlpha(bomb,255) 
        end      
    end 
    local attachedMissiles=getElementData(source,"missileObjs") 
    if attachedMissiles then 
        for index2,missile in pairs(attachedMissiles) do     
            setElementData(missile,"missileAvailable","1") 
            setElementAlpha(missile,255) 
        end      
    end 
end 
addEventHandler ( "onVehicleRespawn", getRootElement(), onVehicleRespawn ) 
  

Client code:

  
addEvent("vehicleGunsChanged",true); 
addEventHandler("vehicleGunsChanged",getRootElement(), 
function(plane) 
    if getElementData(plane,"bombObjs") then 
        local attachedBombs=getElementData(plane,"bombObjs") 
        for index2,obj in pairs(attachedBombs) do    
            setObjectScale(obj,1) 
            setElementCollisionsEnabled(obj,false) 
        end          
    end 
    if getElementData(plane,"missileObjs") then 
        local attachedMissiles=getElementData(plane,"missileObjs") 
        for index2,obj in pairs(attachedMissiles) do     
            setObjectScale(obj,0.5) 
            setElementCollisionsEnabled(obj,false) 
        end          
    end 
end) 
  
  
  
function keyInput(key, keyState ) 
    local player = getLocalPlayer() 
    local vehicle = getPedOccupiedVehicle(player) 
    outputChatBox("pressed") 
    if keyState == "down" then 
        local attachedBombs=getElementData(vehicle,"bombObjs") 
        if attachedBombs then 
            for index2,bomb in pairs(attachedBombs) do   
                if ((getElementData(bomb,"bombAvailable")=="1") and (getElementData(bomb,"bombKey")==key)) then 
                    local x,y,z=getElementPosition(bomb)     
                    z=z-1 
                    local rx,ry,rz=getElementRotation(vehicle) 
                    local vx,vy,vz=getElementVelocity(vehicle)   
                    createProjectile( getLocalPlayer(), 21, x, y, z, 1.0, nil, rx, ry, rz, vx, vy, vz,1636) 
                    setElementData(bomb,"bombAvailable","0") 
                    setElementAlpha(bomb,0) 
                    break 
                end 
            end              
        end 
        local attachedMissiles=getElementData(vehicle,"missileObjs") 
        if attachedMissiles then 
            for index2,missile in pairs(attachedMissiles) do     
                if ((getElementData(missile,"missileAvailable")=="1") and (getElementData(missile,"missileKey")==key)) then 
                    outputChatBox("fire?") 
                    local x,y,z=getElementPosition(missile)  
                    z=z-1 
                    local rx,ry,rz=getElementRotation(missile) 
                    rz=rz+90 
                    local vx,vy,vz=getElementVelocity(vehicle)   
                    createProjectile(getLocalPlayer(), 19, x, y, z, 1.0, nil, rx, ry, rz, vx*3, vy*3, vz*3)  
                    setElementData(missile,"missileAvailable","0") 
                    setElementAlpha(missile,0)       
                    break 
                end 
            end              
        end 
    end 
end 
  
function addKeyBindings(vehicle) 
    outputChatBox("bound") 
    local player = getLocalPlayer() 
    local attachedBombs=getElementData(vehicle,"bombObjs") 
    if attachedBombs then 
        for index2,bomb in pairs(attachedBombs) do   
            unbindKey(getElementData(bomb,"bombKey")) 
            bindKey(getElementData(bomb,"bombKey"),"both",keyInput) 
        end              
    end 
    local attachedMissiles=getElementData(vehicle,"missileObjs") 
    if attachedMissiles then 
        for index2,missile in pairs(attachedMissiles) do     
            unbindKey(getElementData(missile,"missileKey")) 
            bindKey(getElementData(missile,"missileKey"),"both",keyInput) 
        end              
    end 
end 
  
function removeKeyBindings(vehicle) 
    local player = getLocalPlayer() 
    outputChatBox("unbound") 
    local attachedBombs=getElementData(vehicle,"bombObjs") 
    if attachedBombs then 
        for index2,bomb in pairs(attachedBombs) do   
            unbindKey(getElementData(bomb,"bombKey")) 
        end              
    end 
    local attachedMissiles=getElementData(vehicle,"missileObjs") 
    if attachedMissiles then 
        for index2,missile in pairs(attachedMissiles) do     
            unbindKey(getElementData(missile,"missileKey")) 
        end              
    end 
end 
  
  
  
  
addEventHandler("onClientVehicleEnter", getRootElement(), 
    function(thePlayer, seat) 
        if (thePlayer == getLocalPlayer()) and (seat==0) then 
            addKeyBindings(source) 
        end 
    end 
) 
  
addEventHandler("onClientVehicleExit", getRootElement(), 
    function(thePlayer, seat) 
        if (thePlayer == getLocalPlayer()) then 
            removeKeyBindings(source) 
        end 
    end 
) 
  
addEventHandler("onClientPlayerWasted", getLocalPlayer(), 
    function() 
        local vehicle = getPedOccupiedVehicle(player) 
        if (vehicle) then 
            removeKeyBindings(vehicle) 
        end 
    end 
) 
  
  
addEventHandler( "onClientResourceStart", getResourceRootElement( ), 
function() 
    local vehicles = getElementsByType("vehicle") 
    for index,plane in pairs(vehicles) do 
        triggerEvent("vehicleGunsChanged",getRootElement(),plane) 
    end  
end) 
  

When i comment out both createProjectile lines, it doesn't crash. Is there a problem in my script or is this a bug?

Posted

common bug - projectiles explosions are crashing clients sometimes..

Multi theft auto tools - replace cars and peds, move your map or compile your Lua files online!

programista php rzeszów

Need free webhosting for your small site? PM me. Need help with portforwarding? PM me. Do not PM me asking for help with scripting.

Having problems with port forwarding? Send me pm, I can do whole thing for you using TeamViewer (already helped about 20 people, no worries)!

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