Creating a weapon and attaching it on a vehicle

Recommended Posts

Hello there,

I've been trying to create a weapon (uzi) and attach it on a vehicle (hydra) the weapon should fire when I click the left mouse button.

I am stuck at the very beginning of the script (trying to get the uzi to spawn) and I just cannot progress any further.

function hydraGuns()
    local vehicle = getPedOccupiedVehicle(localPlayer)
        if getElementModel(vehicle) == 520 then
            local x, y, z = getElementPosition(vehicle)
            local weapon = createWeapon("uzi", x, y, z)
			outputChatBox ("Hydra guns are installed")

What am I doing wrong?



Share this post

Link to post

Thanks for your reply,

as far as I can see the uzi should spawn when I enter a Hydra. On one point I actually managed to have the uzi spawn, but soon after it didn't work anymore.

would it be better to have the uzi spawn when I press left mouse?


Share this post

Link to post

Spawning a weapon doesn't make it fire.

You can use the function setWeaponState (theWeapon, "firing") to make it fire (i.e when you press the left mouse button)

Share this post

Link to post


I know spawning a weapon doesn't make it fire, but since I am a beginner I try to make my script in little steps.

First try to spawn the weapon > try to attach it to the vehicle > make it fire on left click.

Right now I am stuck on spawning the weapon, that's why I asked for help

Share this post

Link to post


Thanks for your reply but you´re giving me tips on my next step which I haven´t reached yet.

Right now I am trying to get the uzi to spawn, but it doesn´t work for some reason. 

I think the problem might be that the uzi spawns somewhere, but not on the x,y,z coordinates of the player/vehicle.

What could the problem be that the uzi doesn´t spawn?

Share this post

Link to post

Is that your full code (the one you posted)? If so, you never attached that function to an eventHandler. How would MTA know, you want that piece of code to run when the player enters a hydra?

Edited by pa3ck
  • Like 1

Share this post

Link to post

Oh of course I am a retard. I totally forgot that.

I got it working now, an uzi spawns and is attached to the Hydra. Now I will try to make it fire on left mouse button ;)

Thanks for helping me!

Share this post

Link to post

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.