Jump to content

Rocket on Vehicle


Recommended Posts

I create this script but it wont work. It must do when we press ctrl it will fire hunter rocket. I use this script in my map. You can use rocket 1 time in 3 seconds. There is the code its client side.

  
g_Root = getRootElement() 
g_P = getLocalPlayer() 
function onResourceStart(res) 
setElementData(g_P,"rocket",1) 
end 
addEventHandler("onResourceStart",getResourceRootElement(getThisResource()),onResourceStart) 
function rocket (thePlayer) 
if (getElementData(thePlayer,"rocket")==1) then 
createProjectile(getLocalPlayer(),19,getElementPosition(getLocalPlayer()),200) 
rocketT = setTimer(timey,3000,1) 
else 
outputChatBox("You must wait 3 seconds to fire again",thePlayer,0,255,0,true) 
end 
end 
bindKey("ctrl","down",rocket) 
  
function timey () 
setElementData(g_P,"rocket",1) 
killTimer(rocketT) 
end 
  

Link to comment

1. You're using onResourceStart in a client side file it should be onClientResourceStart.

2. Why even bother having some code inside it, and not another bit? I'm talking about that bindKey.

3. g_Root isn't needed as root is a predefined variable.

4. If that element data doesn't need to be known server side, don't sync it (set arg 4 to false)

5. Indent your code so we can follow it (press tab at things like if, function, for)

6. You're using thePlayer inside client side outputChatBox, remove it.

7. thePlayer inside your rocket function won't work because its client side so there is no person arg needed.

Link to comment

also there is no "ctrl" key name, it "lctrl" for left and "rctrl" for right ctrl keys.

also i'd ditched element data and used timer variable to check if player is allowed to shoot:

g_P = getLocalPlayer() 
rocketTimer = false 
  
function rocket() 
  if not rocketTimer then 
    createProjectile(g_P, 19, getElementPosition(g_P), 200) 
    rocketTimer = setTimer(function() rocketTimer = false end, 3000, 1) 
  else 
    outputChatBox("You must wait 3 seconds to fire again", 0, 255, 0, true) 
  end 
end 
  
function onResourceStart() 
  bindKey("lctrl", "down", rocket) 
end 
addEventHandler("onClientResourceStart", resourceRoot, onResourceStart) 

PS: since you want this on a vehicle, you should check if player is in a vehicle and get vehicle positions for projectile.

Link to comment

When i use this

  
    g_P = getLocalPlayer() 
    rocketTimer = false 
    g_V = getPedOccupiedVehicle(g_P) 
  
    function rocket() 
      if not rocketTimer then 
        createProjectile(g_P, 19, getElementPosition(g_V), 200) 
        rocketTimer = setTimer(function() rocketTimer = false end, 3000, 1) 
      else 
        outputChatBox("You must wait 3 seconds to fire again", 0, 255, 0, true) 
      end 
    end 
      
    function onResourceStart() 
      bindKey("lctrl", "down", rocket) 
    end 
    addEventHandler("onClientResourceStart", resourceRoot, onResourceStart) 
  

It wont work and when i use aibo s it blows near my car:)

Link to comment

Maybe above vehicle?

g_P = getLocalPlayer() 
rocketTimer = false 
  
function rocket() 
  if not rocketTimer then 
    local x,y,z = getElementPosition(g_P) 
     
    createProjectile(g_P, 19, x,y,z + 1, 200) 
    rocketTimer = setTimer(function() rocketTimer = false end, 3000, 1) 
  else 
    outputChatBox("You must wait 3 seconds to fire again", 0, 255, 0, true) 
  end 
end 
  
function onResourceStart() 
  bindKey("lctrl", "down", rocket) 
end 
addEventHandler("onClientResourceStart", resourceRoot, onResourceStart) 

Link to comment
  
    g_P = getLocalPlayer() 
    rocketTimer = false 
    g_V = getPedOccupiedVehicle(g_P) 
    rotX,rotY,rotZ = getElementRotation(g_V) 
      
    function rocket() 
      if not rocketTimer then 
        local x,y,z = getElementPosition(g_P) 
        
        createProjectile(g_P, 19, x,y - 10,z + 1, 200,nil,rotX,rotY,rotZ) 
        rocketTimer = setTimer(function() rocketTimer = false end, 3000, 1) 
      else 
        outputChatBox("You must wait 3 seconds to fire again", 0, 255, 0, true) 
      end 
    end 
      
    function onResourceStart() 
      bindKey("lctrl", "down", rocket) 
    end 
    addEventHandler("onClientResourceStart", resourceRoot, onResourceStart) 
  

I scripted this it works but sometimes rocket goes backwards of vehicle. I want it always go infront of vehicle. Like hunter.

Link to comment
g_P = getLocalPlayer() 
rocketTimer = false 
  
function rocket() 
    local g_V = getPedOccupiedVehicle(g_P) 
    local rotX,rotY,rotZ = getElementRotation(g_V) 
    if not rocketTimer then 
        local x,y,z = getElementPosition(g_P) 
    
        createProjectile(g_P, 19, x,y - 10,z + 1, 200,nil,rotX,rotY,rotZ) 
        rocketTimer = setTimer(function() rocketTimer = false end, 3000, 1) 
    else 
        outputChatBox("You must wait 3 seconds to fire again", 0, 255, 0, true) 
    end 
end 
  
function onResourceStart() 
    bindKey("lctrl", "down", rocket) 
end 
addEventHandler("onClientResourceStart", resourceRoot, onResourceStart) 

Getting the vehicle and its rotation had to be inside the firing function.

Link to comment
g_P = getLocalPlayer() 
rocketTimer = false 
  
function rocket() 
    local g_V = getPedOccupiedVehicle(g_P) 
    local rotX,rotY,rotZ = getElementRotation(g_V) 
    if not rocketTimer then 
        local x, y, z = getElementPosition(occupiedVehicle) 
        local matrix = getElementMatrix( g_V) 
        local offX = 0 * matrix[1][1] + 1 * matrix[2][1] + 0 * matrix[3][1] + 1 * matrix[4][1] 
        local offY = 0 * matrix[1][2] + 1 * matrix[2][2] + 0 * matrix[3][2] + 1 * matrix[4][2] 
        local offZ = 0 * matrix[1][3] + 1 * matrix[2][3] + 0 * matrix[3][3] + 1 * matrix[4][3] 
        vx = offX - x 
        vy = offY - y 
        vz = offZ - z 
        x = 0 * matrix[1][1] + 3 * matrix[2][1] + 0 * matrix[3][1] + 1 * matrix[4][1] 
        y = 0 * matrix[1][2] + 3 * matrix[2][2] + 0 * matrix[3][2] + 1 * matrix[4][2] 
        z = 0 * matrix[1][3] + 3 * matrix[2][3] + 0 * matrix[3][3] + 1 * matrix[4][3] 
        createProjectile(g_p, 19, x, y, z, 200, nil, 0, 0, 360 - rotZ, vx, vy, vz) 
        rocketTimer = setTimer(function() rocketTimer = false end, 3000, 1) 
    else 
        outputChatBox("You must wait 3 seconds to fire again", 0, 255, 0, true) 
    end 
end 
  
function onResourceStart() 
    bindKey("lctrl", "down", rocket) 
end 
addEventHandler("onClientResourceStart", resourceRoot, onResourceStart) 

it should create rocket 3m infront of the center of vehicle

Link to comment

occupiedVehicle is nil. use /debugscript 3

also g_p is not g_P

g_P = getLocalPlayer() 

rocketTimer = false

 

function rocket()

    if not rocketTimer and isPedInVehicle(g_P) then

        local occupiedVehicle = getPedOccupiedVehicle(g_P)

        local rotX,rotY,rotZ = getElementRotation(occupiedVehicle)

        local x, y, z = getElementPosition(occupiedVehicle)

        local matrix = getElementMatrix(occupiedVehicle)

        local offX = 0 * matrix[1][1] + 1 * matrix[2][1] + 0 * matrix[3][1] + 1 * matrix[4][1]

        local offY = 0 * matrix[1][2] + 1 * matrix[2][2] + 0 * matrix[3][2] + 1 * matrix[4][2]

        local offZ = 0 * matrix[1][3] + 1 * matrix[2][3] + 0 * matrix[3][3] + 1 * matrix[4][3]

        local vx = offX - x

        local vy = offY - y

        local vz = offZ - z

        x = 0 * matrix[1][1] + 3 * matrix[2][1] + 0 * matrix[3][1] + 1 * matrix[4][1]

        y = 0 * matrix[1][2] + 3 * matrix[2][2] + 0 * matrix[3][2] + 1 * matrix[4][2]

        z = 0 * matrix[1][3] + 3 * matrix[2][3] + 0 * matrix[3][3] + 1 * matrix[4][3]

        createProjectile(g_P, 19, x, y, z, 200, nil, 0, 0, 360 - rotZ, vx, vy, vz)

        rocketTimer = setTimer(function() rocketTimer = false end, 3000, 1)

    else

        outputChatBox("You must wait 3 seconds to fire again", 0, 255, 0, true)

    end

end

 

function onResourceStart()

    bindKey("lctrl", "down", rocket)

end

addEventHandler("onClientResourceStart", resourceRoot, onResourceStart)

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